我之前看到过一个关于如何从字符串列表中查找特定单词的问题。它被删除,我想因为我找不到它了。如何在Python中分解这个列表理解?
因此,例如:
>>>findTheLetters(["hello", "world"], "hold")
>>>True
>>>findTheLetters(["hello", "world"], "holn")
>>>False (because of no "n")
所以,我看到一个帖子有人在这里说,使用列表理解像这样:
return all((any(letter in word for word in myList)) for letter in myString)
我的问题是,我将如何打破这列表理解,所以我可以理解它是如何工作的?我用了简单的(新手)列表理解,但没有那样。
我尝试:
def findTheLetters(myList, myString):
for word in myList:
for letter in word:
#something goes here?
return letter in myString
这是我已经得到最远。它有时像“主”和“持有”一样工作,但就像我尝试“地狱”或“祸”一样,即使字符“h”“e”“l”“l”和“ w“”o“”e“在单词列表中。我不确定我需要添加什么来使其像理解那样工作。
大概可以使用这个答案,除非你关心的字母数,http://stackoverflow.com/questions/9443302/search-strings-in-list-containing-specific-letters-随机顺序 –