[英](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?
問題基本上說明了一切。
我試圖按順序查找包含特定字母的單詞。 因此,如果我搜索“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)
提前致謝!
編輯:
這是我輸入的內容/輸出的內容:(在偽代碼中,它是 uni 的劊子手)
用戶輸入:“你好”——要猜的詞
cpu 猜測:“h”(例如)“h 是你單詞的一部分嗎?(y/n)”
用戶輸入:“y”
計算機知道“h”位於 position 1(通過與必須猜測的單詞進行比較)-愚蠢,我知道,但這是一個例子:D:h _ _ _
現在電腦應該將他所擁有的(h 到索引 0 處的這一點)與數據庫中的單詞(在我的例子中是 words.txt)進行比較,並且只留下首字母為“h”的單詞。 這個過程應該是可以互換的。 例如:如果 pc 有_ _ ll _,他應該仍然可以過濾單詞。
我希望這就是您舉個例子的意思,希望對您有所幫助:D
這僅給出包含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
Output:
['test', 'list']
我想你只是想檢查給定的單詞是否在開頭出現。
substring=substring.lower()
for i in range(0,len(words)): #"words" is a list of 1600 words
s=words[i].lower()
if substring in s: # word found at the begining
sorted.append(words[i])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.