Skip to content

Instantly share code, notes, and snippets.

@Everfighting
Created May 16, 2017 15:39
Show Gist options
  • Save Everfighting/392606d0ae31a8adf4e298237808d246 to your computer and use it in GitHub Desktop.
Save Everfighting/392606d0ae31a8adf4e298237808d246 to your computer and use it in GitHub Desktop.
统计序列中元素出现的频度,及最高的三个值
from random import randint
data = [randint(0,20) for _ in range(30)]
c = dict.fromkeys(data,0)
for x in data:
c[x] += 1
from collections import Counter
# 统计词频
c2 = Counter(data)
# 出现频度最高的n个值
c2.most_common(3)
import re
txt = open('CodingStyle').read()
# 内容分割,进行词频统计
c3 = Counter(re.split('\W+',txt))
# 统计词频最高10个词
c3.most_common(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment