Skip to main content
 首页 » 编程设计

python之计算从文件中读取异常时一行中第一个单词出现的次数

2025年05月04日14小虾米

使用具有以下内容的虚拟文件 (streamt.txt):

andrew I hate mondays. 
fred Python is cool. 
fred Ko Ko Bop Ko Ko Bop Ko Ko Bop for ever 
andrew @fred no it isn't, what do you think @john??? 
judy @fred enough with the k-pop 
judy RT @fred Python is cool. 
andrew RT @judy @fred enough with the k pop 
george RT @fred Python is cool. 
andrew DM @john Oops 
john DM @andrew Who are you go away! Do you know him, @judy? 

每行的第一个词代表一个用户,该行的其余部分是一条消息,类似于 twitter。我需要在他们发送的消息数量旁边打印前 n 个(由用户输入)原始发布用户(大多数消息)的列表。

这不包括任何以“RT”开头的消息。在平局的情况下,按字典顺序在对齐的列中格式化。

就目前而言,我的代码只查找消息中最常用的词,它不排除 RT 和 DM 消息或占 n:

file=open('streamt.txt') 
 
counts=dict() 
for line in file: 
    words=line.split() 
    for word in words: 
    counts[word]=counts.get(word, 0)+1 
 
lst=list() 
for key,value in counts.items(): 
    new=(value, key) 
    lst.append(new) 
 
lst=sorted (lst, reverse=True) 
 
for value, key in lst[:10]: 
    print(value,key) 

这是我的输出:

6 Ko 
5 @fred 
4 andrew 
3 you 
3 is 
3 cool. 
3 RT 
3 Python 
3 Bop 
2 with 

实际输出应该是:

Enter n: 10 
3 andrew 
2 fred 
1 john judy 

关于我应该如何做到这一点有什么想法吗?

请您参考如下方法:

使用Counter :

from collections import Counter 
 
with open(filename, "r") as f: 
    for line in f: 
        if 'DM' not in line and 'RT' not in line: 
            words = line.split() 
            lst.append(words[0]) 
 
for k, v in Counter(lst).items(): 
    print(v, k) 
 
# 2 andrew 
# 2 fred                                                      
# 1 judy