2016-03-06 77 views
2

该单词的行索引和列索引的交叉写功能命名find_word_horizo​​ntal接受字符的 2维列表(如填字游戏)和一个 字符串(单词)作为输入参数。该函数搜索 第2d列表的行以找到匹配的单词。如果找到匹配项,则此函数将返回一个包含匹配起始点的行索引和列索引的列表,否则返回值None(无 报价单)。的Python:查找在2D列表中的单词并返回是在列表

注意:我很抱歉发布一个很长的帖子在这里。我很抱歉,但没有发布适当的问题,我不可能寻求帮助。

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) 

什么我在这里做的是首先把这个词(即“猫”)到一个列表。第二,我已将sublist(即2d列表中的列表)切片以检查三个字母的单词“cat”。我知道我有这种硬编码,但我找不到任何其他方式。上面,这个问题要求以这种方式。

这是我得到的输出:

[3, 0] 

为什么不是if语句更新Column_Index中的价值?切片顺序或什么有问题?任何帮助,将不胜感激。

+0

一个单词可以跨越多行,所以前三个字母在一个列表中,最后两个在下一个?如果不是的话,一个简单的方法可能是将每一行连接在一起并搜索整个字符串,而不是逐字比较字符。如果你认为这会起作用,我可以在以后写一个答案。 –

+0

当然>感谢您的帮助。 –

+0

试试吧,让我知道如果作品如何! –

回答

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] 

让我知道如果您有任何问题!

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] 

它也可能不是一个好主意名称变量“列表”。

+0

上面的代码只适用于三个字母的单词。这是我运行时得到的错误。 #您的功能返回不正确,但您的打印输出正确。 #例如,如下所示调用函数时: #find_word_horizo​​ntal([['a','b'],['c','d']],'cd') #您的函数返回: #None #从函数返回的变量类型为:无类型 #教师函数返回: [1,0] #教师函数返回的变量类型为:列表 –

+0

另外,如何遍历列元素以查找匹配? –

相关问题