2-3课程 比较:统计序列中元素的出现频度
test_1
某随机序列
[1,2,3,4,12,123,44,2,342,123,4,66,35,.....]
中,找到出现次数最高的3个元素,他们出现的次数分别是多少?
方法1
from random import randint
data = [randint(1, 20) for _ in range(30)]
c = dict.fromkeys(data, 0)
for i in data:
c[i] += 1
print(c)
t = list(zip(c.values(), c.keys()))
# 按照key值从小到大排序
print(sorted(t, key=lambda x: x[0]))
# 按照key值从大到小排序
print(sorted(t, key=lambda x: x[0], reverse=True))
# 按照value值从小到大排序
print(sorted(t, key=lambda x: x[1]))
{1: 3, 16: 4, 17: 4, 18: 1, 10: 1, 13: 3, 20: 1, 4: 1, 11: 2, 5: 1, 3: 1, 12: 1, 2: 2, 6: 1, 15: 1, 8: 1, 14: 2}
[(1, 18), (1, 10), (1, 20), (1, 4), (1, 5), (1, 3), (1, 12), (1, 6), (1, 15), (1, 8), (2, 11), (2, 2), (2, 14), (3, 1), (3, 13), (4, 16), (4, 17)]
[(4, 16), (4, 17), (3, 1), (3, 13), (2, 11), (2, 2), (2, 14), (1, 18), (1, 10), (1, 20), (1, 4), (1, 5), (1, 3), (1, 12), (1, 6), (1, 15), (1, 8)]
[(3, 1), (2, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 8), (1, 10), (2, 11), (1, 12), (3, 13), (2, 14), (1, 15), (4, 16), (4, 17), (1, 18), (1, 20)]
[Finished in 0.1s]
方法2
from collections import Counter
data = [randint(1, 20) for _ in range(30)]
c2 = Counter(data)
print(c2)
print(c2.most_common(3))
Counter({7: 4, 8: 3, 19: 3, 9: 2, 3: 2, 5: 2, 4: 2, 16: 2, 13: 2, 15: 1, 17: 1, 20: 1, 2: 1, 12: 1, 10: 1, 6: 1, 11: 1})
[(7, 4), (8, 3), (19, 3)]
[Finished in 0.1s]
test_2
对英文文章的单词,进行词频统计,找到出现次数最高的10个单词,以及他们出现的次数是多少?
import re
from collections import Counter
content = """Hooray! It's snowing! It's time to make a snowman.James runs out. He makes a big pile of snow. He puts a big snowball on top. He adds a scarf and a hat. He adds an orange for the nose. He adds coal for the eyes and buttons.In the evening, James opens the door. What does he see? The snowman is moving! James invites him in. The snowman has never been inside a house. He says hello to the cat. He plays with paper towels.A moment later, the snowman takes James's hand and goes out.They go up, up, up into the air! They are flying! What a wonderful night!The next morning, James jumps out of bed. He runs to the door.He wants to thank the snowman. But he's gone."""
words = re.split('\W+', content)
print(words)
c = Counter(words)
print(c.most_common(10))
#result
['Hooray', 'It', 's', 'snowing', 'It', 's', 'time', 'to', 'make', 'a', 'snowman', 'James', 'runs', 'out', 'He', 'makes', 'a', 'big', 'pile', 'of', 'snow', 'He', 'puts', 'a', 'big', 'snowball', 'on', 'top', 'He', 'adds', 'a', 'scarf', 'and', 'a', 'hat', 'He', 'adds', 'an', 'orange', 'for', 'the', 'nose', 'He', 'adds', 'coal', 'for', 'the', 'eyes', 'and', 'buttons', 'In', 'the', 'evening', 'James', 'opens', 'the', 'door', 'What', 'does', 'he', 'see', 'The', 'snowman', 'is', 'moving', 'James', 'invites', 'him', 'in', 'The', 'snowman', 'has', 'never', 'been', 'inside', 'a', 'house', 'He', 'says', 'hello', 'to', 'the', 'cat', 'He', 'plays', 'with', 'paper', 'towels', 'A', 'moment', 'later', 'the', 'snowman', 'takes', 'James', 's', 'hand', 'and', 'goes', 'out', 'They', 'go', 'up', 'up', 'up', 'into', 'the', 'air', 'They', 'are', 'flying', 'What', 'a', 'wonderful', 'night', 'The', 'next', 'morning', 'James', 'jumps', 'out', 'of', 'bed', 'He', 'runs', 'to', 'the', 'door', 'He', 'wants', 'to', 'thank', 'the', 'snowman', 'But', 'he', 's', 'gone', '']
[('He', 9), ('the', 9), ('a', 7), ('snowman', 5), ('James', 5), ('s', 4), ('to', 4), ('out', 3), ('adds', 3), ('and', 3)]
[Finished in 0.1s]
纸上得来终觉浅,绝知此事要躬行!