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

标签 python string file dictionary count

使用具有以下内容的虚拟文件 (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                                                  

关于python - 计算从文件中读取异常时一行中第一个单词出现的次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50502148/

相关文章:

Python - 如果列表中的任何元素在行中

python - 子表上的 QAbstractItemModel header

python - python 中的 string.replace 方法

python - 无法使用 pyhdb 执行查询

c++ - 字符串比较运算符的含义是什么?

python - R 中是否有类似于 Python 的 % 的字符串格式化运算符?

file - Ansible - 复制是否支持 shell * 扩展

node.js - 无法在 Heroku 服务器上上传图片

python - 在 pandas 的分组条上绘制误差条

python - 找不到满足压缩文件要求的版本(来自版本 : )