3 回答

TA贡献1852条经验 获得超1个赞
从文档中:most_common([n])
返回n个最常见元素的列表,以及从最常见元素到最少元素的计数。如果未指定n,则most_common()返回计数器中的所有元素。相等计数的元素是任意排序的:
>>> 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贡献1775条经验 获得超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
添加回答
举报