【发布时间】:2017-08-01 16:15:38
【问题描述】:
我有一个列表列表:
my_list=[['d','c','d','o','g','q'],
['c','c','u','c','m','w'],
['x','c','c','a','t','t'],
['t','c','e','t','k','e']]
word='cat'
我想找到单词[0] 的位置并将其作为列表返回: word[0]='c',所以我的函数应该返回这个:
[2,2]...
如果我的话是'dog',我的函数会返回:
[0,2]...
我有以下代码,但它不起作用。
def find_word_horizontal(my_list, word):
r=[]
for row in my_list:
x = "".join(row)
s=x.find(word)
if s != -1:
a=my_list.index(row)
r.append(a)
b=x.index(word[0])
r.append(b)
return r
返回:[2,1] 用于 'cat' 而不是 [2,2] 和 [0,1] 用于 'dog' 而不是 [0,2]
【问题讨论】:
标签: python python-3.x