-3
我有一个包含一百个单词的列表和一个八个字母的列表我如何搜索每个字母,找出哪个单词具有最多的单词从列表中选择字母然后打印该字。我如何使用python中的字母列表搜索一个单词列表
我有一个包含一百个单词的列表和一个八个字母的列表我如何搜索每个字母,找出哪个单词具有最多的单词从列表中选择字母然后打印该字。我如何使用python中的字母列表搜索一个单词列表
def searchWord(letters, word):
count = 0
for l in letters:
count += word.count(l)
return count
words = ['hello', 'world'];
letters = ['l', 'o']
currentWord = None
currentCount = 0
for w in words:
n = searchWord(letters, w)
print "word:\t", w, " count:\t", n
if n > currentCount:
currentWord = w
currentCount = n
print "highest word count:", currentWord
不是超级高效的,但你可以做这样的事情:
def search(test, words):
return sorted(((sum(1 for c in word if c in test), word) for word in words),
reverse=True)
这会给你的单词和计数的排序列表。
欢迎来到Stack Overflow!我很抱歉,但我很难弄清楚你在这里问的问题。如果您包含一些代码以显示您尝试过的内容,它会有所帮助,这会让我们更容易帮助您。也许你也可以看一下http://whathaveyoutried.com关于如何提出好问题的伟大文章? –