問題基本上說明了一切。
我正在嘗試按順序查找包含特定字母的單詞。因此,如果我搜索“ers”,它應該只給出“ERStaunding”等詞。
這就是我到目前為止所擁有的:(不起作用,因為字串需要一個字串作為左運算元)但你明白了:
sorted = []
for i in range(0,len(words)): #"words" is a list of 1600 words
if possible in words[i]: #error because of what I said before
sorted.append(words[i]) #"possible" is a string of letters
print(sorted)
提前致謝!
uj5u.com熱心網友回復:
我想你只是想檢查給定的單詞是否在開頭出現。
substring=substring.lower()
for i in range(0,len(words)): #"words" is a list of 1600 words
s=words[i].lower()
if s.find(substring)==0: # word found at the begining
sorted.append(words[i])
uj5u.com熱心網友回復:
這僅給出包含的單詞possible:
words = ["test", "list", "here"] #list of words
possible = "st" #string to find within each word
sortedList = [] #output list defined
for word in words: #for each word
if possible.lower() in word.lower(): #if the word includes the string
sortedList.append(word) #add the word to the output
print(sortedList) #print output
輸出:
['test', 'list']
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/404865.html
標籤:
