问题3:如何统计序列中元素的出现频度
例1:从随机列表中,找到找到出现次数最高的3个元素,及出现次数
方法一:
from random import randint
date = [randint(0, 20) for _ in range(100)]
c = dict.fromkeys(date, 0)
for x in date:
c[x] += 1
c2 = sorted(c.items(), key = lambda k:k[1])
c3 = c2[len(c2)-3:]
print(c3)
- date = [randint(0, 20) for _ in range(100)]:在0~20间,随机生产一个长度100的列表;
- dict.fromkeys(date, 0):以列表的值(不重复使用)做key,以0做值,生产字典;
-
for x in date:c[x] += 1:统计随机list中各元素数量;
- c2 = sorted(c.items(), key = lambda k:k[1]):对统计的元素数量进行排序,以[(key,value)]形式;
- c3 = c2[len(c2)-3:]:返回最后3组数据,为目标结果;
方法二:使用collections下的Counter对象
from collections import Counter
from random import randint
date = [randint(0, 20) for _ in range(100)]
c1 = Counter(date)
c2 = c1.most_common(3)
print(c2)
- Counter(date):直接得到date中元素种类和数量,Counter({0: 7, 14: 7, 15: 7, 17: 7, 13: 6, 11: 6, 12: 5, 6: 5, 8: 5, 9: 5, 20: 4, 16: 4, 1: 4, 19: 4, 7: 4, 3: 4, 2: 4, 18: 3, 5: 3, 4: 3, 10: 3})
- c1.most_common(3),返回出现频率最多的3组数据;
例2:统计一片英文文章中,出现频度最高的10个单词,及出现次数
import re
txt = open('文件x').read()
c = Counter(re.split('W+', txt))
c1 = c.most_common(10)
print(c1)
- txt = open('文件x').read():打开文件x;
- Counter(re.split('W+', txt)):对txt数据进行分割后,得到一个list,并将list内元素种类和数量进行统计;
- c.most_common(10):将字典c1内数量最多的10个元素;