简体   繁体   English

如何从具有字母的行和列中获取单词?

[英]How do i get words from rows and columns having letters?

doing an assignment and struck to this problem 做作业,解决了这个问题

def board_contains_word(board, word):
    '''(list of list of str, str) -> bool

    Return True if and only if word appears in board.

    Precondition: board has at least one row and one column.

    >>> board_contains_word([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 'ANT')
    True
    '''
 return word in board

but i am getting FALSE 但我越来越FALSE

Thanks in advance 提前致谢

The python in operator works a bit differently from how you're using it. python in运算符的工作方式与您使用它的方式有些不同。 Here are some examples: 这里有些例子:

>>> 'laughter' in 'slaughter'
True
>>> 1 in [1,6,5]
True
>>> 'eta' in ['e','t','a']
False
>>> 'asd' in ['asdf','jkl;']
False
>>> 

As you can see, it's got two major uses: testing to see if a string can be found in another string, and testing to see if an element can be found in an array. 如您所见,它有两个主要用途:测试以查看是否可以在另一个字符串中找到一个字符串,以及进行测试以查看是否可以在数组中找到一个元素。 Also note that the two uses can't be combined. 另请注意,这两种用途无法结合使用。

Now, about solving your problem. 现在,关于解决您的问题。 You'll need some sort of loop for going through all of the rows one by one. 您需要某种循环for一遍遍所有行。 Once you've picked out a single row, you'll need some way to join all of the array elements together. 一旦你选择了单行,你需要一些方法来join所有的数组元素在一起。 After that, you can figure out if the word is in the board. 之后,您可以确定单词是否in面板中。

Note: this only solves the problem of searching horizontally. 注意:这仅解决了水平搜索的问题。 Dunno if that's the whole assignment. Dunno,如果那是整个任务。 You can adapt this method to searching vertically using the zip function. 您可以使用zip功能使此方法适应垂直搜索。

Here's something to get you unstuck: 这里有一些让您解脱的东西:

def board_contains_word(board, word):
    # check accross
    for row in board:
        return word in ''.join(row):

    # try with board's rows and columns transposed
    for row in zip(*board):
        return word in ''.join(row):

    return False

print board_contains_word([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 'ANT')
print board_contains_word([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 'TO')

Hint: You could simplify things by using the any() function. 提示:您可以使用any()函数简化事情。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何让这个代码计算单词而不是字母? - How do i get this code to count words not letters? 为什么我得到字母而不是单词的频率? - Why do I get a frequency of letters and not words? 如何从具有特定字母的列表中查找单词? - How do I find words from a list with specific letters? 我如何从多个单词中得到第一个字母,然后再从*个剩余字母中得到*? - How can I get the first letter from multiple words and * the remaining letters? 如何在列表中交替使用字母/单词的大小写? - How do I alternate the cases of letters/words in a list? 如何使用在 python 中作为变量输入的日期从指定行内的 csv 中获取列的总和? - How do I get the sum of columns from a csv within specified rows using dates inputting as variables in python? 如何在行而不是列中获得 sklearn.standardscale? - How do I get sklearn.standardscale in rows and not the columns? Pandas 不能识别单词,只能识别字母。 当我切片时我该怎么做,它给我的是单词而不是字母? - Pandas is not recognizing words, but only letters. How can I do so when I slice, it gives me the words not the letters? 如何开始在 Python Pandas 中将列转换为行? - How do I get started with turning columns into rows in Python Pandas? (Python)如何将几个字母与整个单词列表进行比较以查找哪些单词按顺序包含搜索的字母? - (Python) How do I compare a few letters to a whole list of words to find, what words contain the searched-for letters in order?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM