Python:在二维列表中查找一个单词,并返回交集,即该单词在列表中的行索引和列索引

lstz6jyr  于 2022-11-26  发布在  Python
关注(0)|答案(5)|浏览(151)

编写一个名为find_word_horizontal的函数,该函数接受一个二维字符列表(类似于填字游戏)和一个字符串(word)作为输入参数。该函数搜索二维列表中的行,以找到与该单词匹配的行。如果找到匹配,则该函数返回一个包含匹配开始的行索引和列索引的列表,否则返回值None(无引号)。

    • 注意:我很抱歉在这里发布了一个很长的帖子。我很抱歉,但没有张贴适当的问题,这是不可能的,我要求帮助。**
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")转换成一个列表。其次,我对sublist(即2d列表中的列表)进行切片,以检查三个字母的单词"cat"。我知道我已经对它进行了某种程度的硬编码,但我找不到任何其他方法。最重要的是,问题要求它以这种方式进行。
下面是我在输出中得到的结果:

[3, 0]

为什么if语句没有更新column_index的值?切片顺序有问题吗?如果有任何帮助,我们将不胜感激。

t2a7ltrp

t2a7ltrp1#

希望这能帮上忙,我留下了一些指纹,这样你就能看到发生了什么:

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]

如果您有任何问题,请告诉我!

drkbr07n

drkbr07n2#

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]

将变量命名为“list”也可能不是一个好主意。

q5iwbnjs

q5iwbnjs3#

另一种解决方法:

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
fgw7neuy

fgw7neuy4#

以下代码对我很有效:

def find_word_horizontal(crossword,word):  
    global row_index  
    number_of_rows=len(crossword)  
    number_of_columns=len(crossword[0])  
    row_items_list=[]  
    final_output=[]  
    for i in range(0,number_of_rows):  
        row_items=""  
        for j in range(0,number_of_columns):  
            row_items+=crossword[i][j]  
        row_items_list.append(row_items)  
    for i in row_items_list:  
        if word in i:  
            row_index=row_items_list.index(i)  
            for k in range(0,number_of_columns):  
                if(word[0]==crossword[row_index][k]):  
                    column_index=k  
                    final_output.append(row_index)  
                    final_output.append(column_index)  
                    return final_output  
                    break  
        else:  
            continue  

crosswords=[['s','d','o','g'],['c','u','c','m'],['a','c','a','t'],['t','e','t','k']]  
word='car'  
print(find_word_horizontal(crosswords,word))
rdrgkggo

rdrgkggo5#

这是可以的,但如果你发现错误,请告诉我。谢谢

def find_word_horizontal(a_list, w):
address = list()
for row in a_list:
    chek = ""
    for char in row:
        chek = chek + char
    #print(chek)
    if w in chek:
        address.append(a_list.index(row))
        address.append(chek.find(w))
        break
return address

相关问题