【发布时间】:2023-03-14 01:26:01
【问题描述】:
正如标题所说,我需要编写一个代码来返回频率最高的 3 个单词(来自输入字符串)的列表。这是我目前所拥有的:
输入:
import collections
print(sstr)
输出:
['22574999', 'communication was sent']
['22582857', 'message originated from an industrial area in pacoima']
['22585166', 'your message will never be delivered']
['22585424', 'message has been delivered ']
在:
import collections
id = sstr[0]
info = (sstr[1]).split()
print(id,info)
输出:
22574999 ['communication', 'was', 'sent']
22582857 ['message', 'originated', 'from', 'an', 'industrial', 'area', 'in', 'pacoima']
22585166 ['your', 'message', 'will', 'never', 'be', 'delivered']
22585424 ['message', 'has', 'been', 'delivered']
在:
import collections
id = sstr[0]
info = (sstr[1]).split()
c = collections.Counter()
for word in info:
c[word] += 1
print(c.most_common(3))
输出:
Counter({'communication': 1, 'was': 1, 'sent': 1})
Counter({'message': 1, 'originated': 1, 'from': 1, 'an': 1, 'industrial': 1, 'area': 1, 'in': 1, 'pacoima': 1})
Counter({'your': 1, 'message': 1, 'will': 1, 'never': 1, 'be': 1, 'delivered': 1})
Counter({'message': 1, 'has': 1, 'been': 1, 'delivered': 1})
我想将所有行合并为一个并找到频率最高的前 3 个单词。 以及如何找到频率最高的前3个单词的id总和?
我想得到以下结果
结果:
top 3 words with highest frequency:
message :3
delivered:2
communication:1
sum of id in which there аре top 3 words with highest frequency:
message:3 Is included (22582857,22585166,22585424 )
delivered:2 Is included(22585166,22585424)
communication:1 Is included (22574999)
【问题讨论】:
-
那么.. 是什么阻止你写它?
-
循环遍历 sstr 的值并将所有单词添加到一个 Counter 中,而不是为每一行创建一个单独的 Counter。
标签: python python-3.x