【问题标题】:Check if a letter is in a bidimensional list检查一个字母是否在二维列表中
【发布时间】:2012-04-04 15:49:52
【问题描述】:

我创建了一个列表,其中包含 abecedary 以及二维列表中的相关单词。 但是当我尝试查找列表中包含的单词并打印相关单词时,它会抛出我:

TypeError: list indices must be integers, not list

这是我的代码:

import parallel
import time
import string

abc=[['a','EB'], ['b','F8']]

print ("Write something: ")
text = raw_input()
lent=len(text)
print (lent)
p=parallel.Parallel()
text1=list(text)

for x in text1:
print (x)
    i=0
for i in abc:
    if x in abc[0][i]:
           print(abc[0][i])
       p.setData(int('0x'+abc[0][i],16))

 time.sleep(0.5)

【问题讨论】:

    标签: python list integer indices


    【解决方案1】:
    >>> abc = [['a','EB'],['b','F8']]
    >>> for i in abc:
    ...    print i
    ...
    ['a', 'EB']
    ['b', 'F8']
    

    所以你可能需要这个:

    for i in abc:
        if x == i[0]:
               print(i[1])
    

    【讨论】:

      【解决方案2】:

      类型错误在这里

       if x in abc[0][i]:
      

      i 将是一个列表,第一遍i 将是['a','EB'],第二遍i 将是['b','F8']

      用另一个列表来索引一些东西是没有意义的,你可能认为i 是索引,但在 python 中,for 循环将循环遍历值。

      【讨论】:

        猜你喜欢
        • 2014-12-08
        • 1970-01-01
        • 1970-01-01
        • 2022-01-05
        • 1970-01-01
        • 1970-01-01
        • 2019-12-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多