0

我创建了一个列表,其中包含 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)
4

2 回答 2

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])
于 2012-03-20T06:48:52.267 回答
0

类型错误在这里

 if x in abc[0][i]:

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

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

于 2012-03-20T06:47:58.287 回答