码迷,mamicode.com
首页 > 其他好文 > 详细

2-3课程 比较:统计序列中元素的出现频度

时间:2020-06-22 13:07:42      阅读:47      评论:0      收藏:0      [点我收藏+]

标签:rand   port   比较   ever   mes   comm   key   词频统计   out   

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]

2-3课程 比较:统计序列中元素的出现频度

标签:rand   port   比较   ever   mes   comm   key   词频统计   out   

原文地址:https://www.cnblogs.com/botoo/p/13176126.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!