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]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

软件测试技术

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值