【发布时间】:2017-02-13 05:40:07
【问题描述】:
我之前看到过一个关于如何从字符串列表中找到特定单词的字符的问题。我想它被删除了,因为我再也找不到它了。
例如:
>>>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
这是我到过的最远的地方。它有时像“lord”和“hold”一样工作,但就像我尝试“hell”或“woe”一样,即使字符“h”“e”“l”“l”和“ w" "o" "e" 在单词列表中。我不确定我需要添加什么才能使它像理解一样工作。
【问题讨论】:
-
可能会使用这个答案,除非你关心字母的数量,stackoverflow.com/questions/9443302/…
标签: python string list python-3.x list-comprehension