繁体   English   中英

(Python)如何将几个字母与整个单词列表进行比较以查找哪些单词按顺序包含搜索的字母?

[英](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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM