python 统计序列中元素的出现频度\统计英文文章的词频

一:

In [28]: data=[random.randint(1,20) for _ in range(30)]

In [29]: d = dict.fromkeys(data,0)

In [32]: for i in data:

    ...:     d[i] += 1


二:

In [34]: from collections import Counter
In [35]: d2=Counter(data)

In [37]: d2.most_common(3)#出现频度前3的值和出现次数


三:

In [55]: t=open('a.txt','r')

In [58]: t1=t.read()
In [59]: c1=re.split('[^\w]+',t1)

In [74]: c2 =Counter(c1)

In [75]: c2.most_common(3)



你可能感兴趣的:(python高级)