【Python教程】统计序列中元素出现频度的详细方法

例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对象

'''
学习中遇到问题没人解答?小编创建了一个Python学习交流QQ群:531509025
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
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个元素;

猜你喜欢

转载自blog.csdn.net/qdPython/article/details/120491418