python(统计元素中出现的频度)

案例


(1)某随机序列[12, 5, 6, 4, 6, 5, 5, 7, ....]中,找到出现次数最高的3个元素,他们出现的次数是多少?
(2)对某英文文章的单词,进行词频统计,找到出现次数最高的10个单词,他们出现的次数是多少?

解析

from random import randint

#--------------------dict.fromkeys(data, 0)---------------------------------#
data = [randint(0, 20) for _ in range(30)]
print(data)

c = dict.fromkeys(data, 0)  # 快速创建字典
print(c)

for x in data:
    c[x] += 1
print(c)

#---------------------data.count--------------------------------#
data_set = list(set(data))  # 创建一个过滤好的set列表

for x in data_set:
    c[x] = data.count(x)   # 找到过滤好的set中的每一个元素,在原始列表中出现的次数

print(c)

#----------------------collections Counter-------------------------------#
from collections import Counter  
c2 = Counter(data)
print(c2)  # 输出结果,已经按照频度排序
c3 = Counter(data).most_common(3)   #选出频度最高的前3名
print(c3)


#----------------------正则 split()--------------------------------#
import re  
with open('CodingStyle.txt', 'r') as f:
    txt = f.read()
    c4 = Counter(re.split('\W+', txt))
    # print(c4)
    # print(c4.most_common(10))


你可能感兴趣的:(python(统计元素中出现的频度))