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]
纸上得来终觉浅,绝知此事要躬行!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
2019-06-22 unittest===unittest 的几种执行方式