我之前看到過一個關於如何從字符串列表中查找特定單詞的問題。它被刪除,我想因爲我找不到它了。如何在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-隨機順序 –