使用多维列表帮助下面的代码我需要搜索数组中的每一行字符并找到一个单词并返回第一个字母的位置。这适用于一个字母,但是当我尝试以字母开头的单词而不是整个单词时,它不起作用。有人可以帮助我吗?我可以看到,我从x = word [0]开始,但每当我尝试增加x的值以包含我时,都会搞乱。基本上我需要'aca'返回[2,1],因为这是单词中第一个字符的开头,但是我输入的是ara应该是返回值。多维列表搜索
def find_word_horizontal(crosswords,word):
word=str(word)
x=word[0]
char=0
crosslen=len(crosswords)
found=False
for row in crosswords:
r=crosswords.index(row)
wordcount=len(word)
for i in range(0, len(row)):
if wordcount<2:
if row[i]==x:
r=crosswords.index(row)
found=True
print([r, i])
return([r, i])
else:
for i in range(0, len(row)):
for let in word:
if row[i]==let and char<=wordcount:
char+=1
if char==wordcount:
t=0
for c in row:
if c==x:
found=True
r=crosswords.index(row)
print([r, t])
return([r, t])
t+=1
char=0
else:
continue
if found==False:
print(None)
return None
crosswords=[['s', 'd', 'o', 'g'], ['c', 'u', 'c', 'm'], ['a', 'c', 'a', 't'], ['t', 'e', 't', 'k']]
word='aca'
find_word_horizontal(crosswords,word)
欢迎来到StackOverflow。请阅读并遵守帮助文档中的发布准则。 [最小,完整,可验证的示例](http://stackoverflow.com/help/mcve)适用于此处。在发布代码*和*准确描述问题之前,我们无法有效帮助您。 “它不工作”和“我一直搞乱”不是问题描述。单字母变量名称和没有描述性注释将您的代码作为猜谜游戏留给其他人。 – Prune
另外,为什么当单词从[2,0]开始时期望[2,1]?位置[2,1]是'c'。你的代码已经为任何以'a'开头的东西产生了[2,0] - 这是一个开始。 – Prune
您可能还会从[如何调试](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)中获取一些提示。 – Prune