目标:
寻找一个列表中,哪个单词含有最多的重复的字母。
注意是,排序的依据是字母,但是查找的是单词。
答案是:
from collections import Counter
import operator
WORDS = ['this', 'is', 'an',
'elementary', 'test', 'example']
def most_repeating_letter_count(word):
return Counter(word).most_common(1)[0][1]
def most_repeating_word(words):
return max(words, key=most_repeating_letter_count)
print(most_repeating_word(WORDS))
但是我有个疑问,Counter(word).most_common(1)[0][1]到底算是个什么对象?
因为按我的理解:
1.Counter(word).most_common() 显然是计数”单词“的重复数,而不是字母的,结果应该是:
[('this', 1), ('is', 1), ('an', 1), ('elementary', 1), ('test', 1), ('example', 1)]
2.那Counter(word).most_common(1)选的是重复数最多的”单词“,那么到底是哪一个?
3.即使2中选了一个,因为”单词“都是只出现了一次,所以Counter(word).most_common(1)[0][1]最后无论如何的结果都是一个数字:1
4.所以这特么怎么能去支撑后面max()的排序的呢?我到底哪里想错了?