3 回答

TA貢獻(xiàn)1852條經(jīng)驗 獲得超1個贊
從文檔中:most_common([n])
返回n個最常見元素的列表,以及從最常見元素到最少元素的計數(shù)。如果未指定n,則most_common()返回計數(shù)器中的所有元素。相等計數(shù)的元素是任意排序的:
>>> Counter('abracadabra').most_common(3)
[('a', 5), ('r', 2), ('b', 2)]
您的代碼可以是:
from collections import Counter
c = Counter(most_used_words)
msg = "Here is your breakdown of your most used words:\n\nWords | Times Used\n:--:|:--:\n"
msg += '\n'.join('%s|%s' % (k.capitalize(), v) for (k, v) in c.most_common(10))
r.send_message(user, 'Most Used Words', msg)

TA貢獻(xiàn)1775條經(jīng)驗 獲得超8個贊
一旦有了values它,就很簡單:
print('Word | Times Used')
for e, t in collections.Counter(values).most_common(10):
print("%s|%d" % (e,t))
打印類似:
Word | Times Used
e|4
d|3
a|2
c|2
添加回答
舉報