Python统计字符串单词出现次数并输出Top 5
Python统计字符串单词出现次数并输出Top 5
本文将介绍如何使用Python统计字符串中每个单词出现的次数,并输出出现次数排名前五名的单词。
任务描述:
已知字符串变量 s 存储了一段关于北京2022年冬奥会的文本信息:
'According to independent research conducted on behalf of the IOC, Beijing 2022 has become the most digitally engaged Olympic Winter Games in history, with 2.01 billion viewers, up 5% from PyeongChang 2018 watching the Games through linear TV and digital platforms. Viewers watched a total of 713 billion minutes of the Games coverage through Olympic media rights partners' channels, and Olympic social media also achieved 3.2 billion engagements and attracted more than 11 million new followers across various platforms.'
我们需要编写程序实现以下功能:
- 统计字符串中每个单词出现的次数,并将结果输出;2. 输出出现次数排名前五名的单词。
**Python实现:**pythons = 'According to independent research conducted on behalf of the IOC, Beijing 2022 has become the most digitally engaged Olympic Winter Games in history, with 2.01 billion viewers, up 5% from PyeongChang 2018 watching the Games through linear TV and digital platforms. Viewers watched a total of 713 billion minutes of the Games coverage through Olympic media rights partners' channels, and Olympic social media also achieved 3.2 billion engagements and attracted more than 11 million new followers across various platforms.'
将字符串按空格分割成单词列表words = s.split()
统计每个单词出现的次数word_count = {}for word in words: if word in word_count: word_count[word] += 1 else: word_count[word] = 1
输出每个单词出现的次数print('每个单词出现的次数:')for word, count in word_count.items(): print(f'{word}: {count}')
输出出现次数排名前五名的单词top_words = sorted(word_count, key=word_count.get, reverse=True)[:5]print('\n出现次数排名前五名的单词:')for word in top_words: print(f'{word}: {word_count[word]}')
代码解析:
- 首先,我们将字符串
s按空格分割成单词列表,存储在变量words中。2. 接着,我们创建一个空字典word_count用于存储每个单词出现的次数。3. 遍历单词列表words,如果单词已经存在于字典word_count中,则将其对应的计数值加 1;否则,将该单词作为键添加到字典中,并将计数值初始化为 1。4. 最后,我们分别输出字典word_count中的所有键值对,以及根据值排序后的前五个键值对,即出现次数排名前五名的单词及其出现次数。
输出结果:
每个单词出现的次数:According: 1to: 1independent: 1research: 1conducted: 1on: 1behalf: 1of: 4the: 3IOC,: 1Beijing: 12022: 1has: 1become: 1most: 1digitally: 1engaged: 1Olympic: 2Winter: 1Games: 2in: 1history,: 1with: 12.01: 1billion: 3viewers,: 1up: 15%: 1from: 1PyeongChang: 12018: 1watching: 1through: 2linear: 1TV: 1and: 3digital: 1platforms.: 2Viewers: 1watched: 1a: 1total: 1713: 1minutes: 1coverage: 1media: 1rights: 1partners': 1channels,: 1also: 1achieved: 13.2: 1engagements: 1attracted: 1more: 1than: 111: 1million: 1new: 1followers: 1across: 1various: 1
出现次数排名前五名的单词:of: 4the: 3billion: 3and: 3Olympic: 2
通过以上代码,我们成功地统计了字符串中每个单词出现的次数,并输出了出现次数排名前五名的单词。
原文地址: https://www.cveoy.top/t/topic/PI3 著作权归作者所有。请勿转载和采集!