我有一个包含93个不同字符串的列表.我需要找到10个最常见的字符串,并且返回必须按顺序从最频繁到最不频繁.
mylist = ['"and', '"beware', '`twas', 'all', 'all', 'and', 'and', 'and', 'and', 'and', 'and', 'and', 'and', 'and', 'and', 'and', 'and', 'and', 'arms', 'as', 'as', 'awhile', 'back', 'bandersnatch', 'beamish', 'beware', 'bird', 'bite', 'blade', 'borogoves', 'borogoves', 'boy', 'brillig']
# this is just a sample of the actual list.
Run Code Online (Sandbox Code Playgroud)
我没有最新版本的python,不能使用计数器.
Dav*_*ber 16
你可以使用一个Counter从collections模块做到这一点.
from collections import Counter
c = Counter(mylist)
Run Code Online (Sandbox Code Playgroud)
然后c.most_common(10)回来
[('and', 13),
('all', 2),
('as', 2),
('borogoves', 2),
('boy', 1),
('blade', 1),
('bandersnatch', 1),
('beware', 1),
('bite', 1),
('arms', 1)]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5449 次 |
| 最近记录: |