我有一个大约一百个单词的列表和一个八个字母的列表我如何搜索每个字母,找出哪个单词列表中的字母最多,然后打印该单词。
答案 0 :(得分:1)
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
答案 1 :(得分:0)
效率不高,但你可以这样做:
def search(test, words):
return sorted(((sum(1 for c in word if c in test), word) for word in words),
reverse=True)
那将为您提供一个排序的单词和计数列表。