Python:在2d列表中查找单词并返回交集,该交集是列表中该单词的行索引和列索引

时间:2016-03-06 06:19:42

标签: python-3.x

  

编写一个名为find_word_horizo​​ntal的函数,它接受一个   二维字符列表(如填字游戏)和一个   string(word)作为输入参数。此函数搜索行   2d列表找到该单词的匹配项。如果找到匹配,这个   函数返回一个包含行索引和列索引的列表   匹配开始,否则返回值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”。我知道我有点硬编码,但我找不到任何其他方式。 Tops,问题要求它以这种方式。

这就是我在输出中得到的结果:

[3, 0]

为什么if语句没有更新column_index的值?切片顺序是有问题还是什么?任何帮助将不胜感激。

4 个答案:

答案 0 :(得分:1)

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"。

可能也不是一个好主意

答案 1 :(得分:1)

希望这有帮助,我留下了一些印刷品,以便你可以看到发生了什么:

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]

如果您有任何疑问,请与我联系!

答案 2 :(得分:0)

另一种解决方法:

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

答案 3 :(得分:0)

此代码对我有用:

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))