Python 3.x Python:在2d列表中查找一个单词,并返回该单词在列表中的行索引和列索引的交点
编写一个名为find_word_horizontal的函数,该函数接受 二维字符列表(如填字游戏)和 字符串(word)作为输入参数。此函数用于搜索 二维列表以查找与该单词匹配的项。如果找到匹配项,则此 函数返回一个列表,其中包含 开始匹配,否则返回值None(否 引文) 注意:我很抱歉在这里发布了一篇长文章。非常抱歉,如果没有适当的问题,我无法寻求帮助。Python 3.x Python:在2d列表中查找一个单词,并返回该单词在列表中的行索引和列索引的交点,python-3.x,Python 3.x,编写一个名为find_word_horizontal的函数,该函数接受 二维字符列表(如填字游戏)和 字符串(word)作为输入参数。此函数用于搜索 二维列表以查找与该单词匹配的项。如果找到匹配项,则此 函数返回一个列表,其中包含 开始匹配,否则返回值None(否 引文) 注意:我很抱歉在这里发布了一篇长文章。非常抱歉,如果没有适当的问题,我无法寻求帮助。 For example if the function is called as shown below: > > cross
For example if the function is called as shown below:
>
> crosswords=[['s','d','o','g'],['c','u','c','m'],['a','c','a','t'],['t','e','t','k']]
> word='cat'
>
> find_word_horizontal(crosswords,word)
>
> then your function should return [2,1]
>
> Notice that the 2d input list represents a 2d crossword and the
> starting index of the horizontal word 'cat' is [2,1]
Note: In case of multiple matches only return the match with lower row index. If you find two matches in the same row
then return the match with lower column index
我已经写了这段代码。这可能不是最好的代码,但是:
def find_word_horizontal (crosswords, word):
list = []
output_list = []
row_index = -1
column_index = 0
list = word.split()
for sublist in crosswords:
if (sublist[1:] == list[:] or sublist[0:-1] == list[:]):
column_index += 1
row_index += 1
output_list.append(row_index)
output_list.append(column_index)
return (output_list)
#Main Program
crosswords = [['s','d','o','g'],['c','u','c','m'],['a','c','a','t'],['t','e','t','k']]
word = 'cat'
result = find_word_horizontal(crosswords,word)
print (result)
我在这里做的是首先将单词(即“cat”)转换成一个列表。其次,我将子列表
(即2d列表中的一个列表)切片,以检查三个字母的单词“cat
”。我知道我有点硬编码,但我找不到任何其他方法。最重要的是,问题要求它是这样的
这是我在输出中得到的:
[3, 0]
为什么if语句不更新列索引的值?切片顺序有问题吗?任何帮助都将不胜感激。word.split()
不会将单词拆分为字符列表,但list(word)
会。然后在获取索引时有一个轻微的逻辑缺陷,但是循环中的enumerate
在这里很有用
def find_word_horizontal (crosswords, word):
input_list = list(word)
output_list = []
row_index = -1
column_index = 0
for outer_index, sublist in enumerate(crosswords):
for inner_index in xrange(0,(len(sublist)-len(input_list)+1)):
if sublist[inner_index:inner_index+len(input_list)]==input_list:
return [outer_index,inner_index]
将变量命名为“列表”也可能不是一个好主意。希望这能有所帮助,我留下了一些打印,以便您可以看到发生了什么:
def find_word_horizontal (crosswords, word):
for row_index, row in enumerate(crosswords):
print('input: ', row_index, row)
row_string = ''.join(row)
print('joined row: ', row_string)
column_index = row_string.find(word)
if(column_index > -1):
return [row_index, column_index]
find_word_horizontal(crosswords, word)
输出:
input: 0 ['s', 'd', 'o', 'g']
joined row: sdog
input: 1 ['c', 'u', 'c', 'm']
joined row: cucm
input: 2 ['a', 'c', 'a', 't']
joined row: acat
Out[5]: [2, 1]
如果你有任何问题,请告诉我 另一种解决方法:
def find_word_horizontal(crossword,word):
myindex = []
for element in crossword :
row = ''.join(element)
if word in row :
myindex.append(crossword.index(element))
myindex.append(row.index(word))
return myindex
这个代码对我有用:
def find_word_horizontal(纵横字谜、单词):
全局行索引
行数=len(纵横字谜)
列数=len(纵横字谜[0])
行项目列表=[]
最终输出=[]
对于范围内的i(0,行数):
row_items=“”
对于范围内的j(0,列数):
行项目+=纵横字谜[i][j]
行项目列表。追加(行项目)
对于第i行中的项目列表:
如果i中的单词:
行索引=行项目列表。索引(i)
对于范围内的k(0,列数):
如果(字[0]==纵横字谜[行索引][k]):
列索引=k
最终输出。追加(行索引)
最终输出。追加(列索引)
返回最终输出
打破
其他:
继续
纵横字谜=['s'、'd'、'o'、'g']、['c'、'u'、'c'、'm']、['a'、'c'、'a'、't']、['t'、'e'、't'、'k']
“汽车”
打印(水平查找单词(纵横字谜、单词))
一个单词可以跨多行扩展,因此前三个字母在一个列表中,后两个在下一个列表中?如果不是,一种简单的方法可能是将每一行连接在一起,搜索整个字符串,而不是逐个字符进行比较。如果你认为这有用,我可以稍后写一个答案。当然>谢谢你的帮助。试试看,让我知道如果有用!上面的代码只适用于三个字母的单词。这是我运行它时遇到的错误#函数返回不正确,但打印输出正确#例如,当按如下所示调用函数时:#find#word#u horizontal(['a',b',['c',d']],'cd')#函数返回:#None#函数返回的变量类型为:NoneType#讲师的函数返回:[1,0]#讲师函数返回的变量类型为:ListAlway如何遍历列元素以查找匹配项?