Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:单词搜索_Python - Fatal编程技术网

Python:单词搜索

Python:单词搜索,python,Python,因此,我一直在尝试为python中的单词搜索解算器编写代码。鉴于此,我一直在代码的某一特定部分遇到问题,该部分如下: first_words=[] final_words=[] first_row={} final_row={} first_column={} final_column={} for x in words: for y in x[0]: first_words.append(y) print(first_words) for x in words:

因此,我一直在尝试为python中的单词搜索解算器编写代码。鉴于此,我一直在代码的某一特定部分遇到问题,该部分如下:

first_words=[]
final_words=[]
first_row={}
final_row={}
first_column={}
final_column={}
for x in words:
    for y in x[0]:
        first_words.append(y)
print(first_words)

for x in words:
    for y in x[-1]:
        final_words.append(y)
print(final_words)

for z in rows:     
    for x in first_words:
        if x in rows[rows.index(z)]:
            first_row.update({x:rows.index(z)+1})
print(first_row)

for z in rows:
    for x in final_words:
        if x in rows[rows.index(z)]:
            final_row.update({x:rows.index(z)+1})
print(final_row)

for z in rows:
    for w in z:
        for x in first_words:
            if x in z[z.index(w)]:
                first_column.update({x:z.index(w)+1})
print(first_column)
因此,基本上这一部分会选取单词的首字母和末字母,然后交叉检查字母在网格中的索引。我把每个索引都存储在字典里。现在我的问题是,它似乎读取了错误的索引。例如,对于给定的单词:“Tea”和“Sit”,第一个字母索引似乎没有问题,但最后一个字母索引关闭,因为正在读取“Tea”的字母“T”的索引,而不是“Sit”中的字母“T”。我该如何解决这个问题?任何帮助都将不胜感激

视觉指南:

['mon', 'sit', 'tea'] #words to find
['MON', 'SIT', 'TEA'] #for simplification in error checking I used the same strings to populate my 
                       board
  1 2 3
1 M O N
2 S I T  #The board
3 T E A

['M', 'S', 'T'] #first letter of the words
['N', 'T', 'A'] #last letter of the words
{'M': 1, 'S': 2, 'T': 3} #dictionary for the indexes of the first letter(row)
{'N': 1, 'T': 3, 'A': 3}  #dictionary for the indexes of the last letter(row)
{'M': 1, 'S': 1, 'T': 1}  #dictionary for the indexes of the first letter(column)
{'N': 3, 'T': 1, 'A': 3}  #dictionary for the indexes of the lastletter(column)
words= ['MON', 'SIT', 'TEA']
rows= ['MON', 'SIT', 'TEA']

我建议你同时做每件事:

for x in words:
    for index,y in enumerate(x[0]):
        first_words.append(y)
        first_row.update({y : index+1})
对其他人也这样做,应该没问题。 这种方法更简单,更不容易出错