计算一组字符串出现的次数,并在python 2.7中打印字符串和出现的次数 [英] Counting number of times a group of string have occurred and print the string and number of occurrence in python 2.7

查看:116
本文介绍了计算一组字符串出现的次数,并在python 2.7中打印字符串和出现的次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我试图逐行读取文本文件,并检查一行中每组字符串的出现次数。文本文件包含这些行(各不相同)

I am trying to read a text file line by line and check the number of occurrence of each group of strings in a line for example. A text file contains these lines (Which varies)

X_0_Gui_Menu_400_Menu_System
X_0_Gui_Menu_400_Menu_System
X_0_Gui_Menu_000_Menu_root
X_0_Gui_Menu_000_Menu_root
X_0_Gui_Menu_000_Menu_root
X_0_Gui_Menu_300_Menu_Outputs
X_0_Gui_Menu_300_Menu_Outputs
X_0_Gui_Menu_320_Menu_Outputs_SDI
X_0_Gui_Menu_320_Menu_Outputs_SDI
X_0_Gui_Menu_320_Menu_Outputs_SDI
X_0_Gui_Menu_320_Menu_Outputs_SDI
X_0_Gui_Menu_320_Menu_Outputs_SDI
X_0_Gui_Menu_320_Menu_Outputs_SDI
X_0_Gui_Menu_320_Menu_Outputs_SDI
X_0_Gui_Menu_320_Menu_Outputs_SDI

我想获得

X_0_Gui_Menu_400_Menu_System
2 times

X_0_Gui_Menu_000_Menu_root
3 times

X_0_Gui_Menu_300_Menu_Outputs
2 times

X_0_Gui_Menu_320_Menu_Outputs_SDI
8 times

我看到了一些论坛使用计数,但我不知道wh at数据将存在于文件中以进行输入检查,但这完全是随机的。所以我很困惑如何实现这一点。任何指导都将真正有用。

I saw some forums using count, but I dont know what data will be there in the file to give an input to check, but it is completely random. So I am confused how to implement this. Any guidance would be really helpful.

推荐答案

您可以通过使用收集模块。在Python文档中:计数器是用于对可哈希对象进行计数的dict子类。它是一个无序集合,其中元素存储为字典键,其计数存储为字典值。计数可以是任何整数值,包括零或负count类。Counter类类似于其他语言的bag或multiset。

You can achieve this by using the Counter container from the collection module. From the Python documentation: "A Counter is a dict subclass for counting hashable objects. It is an unordered collection where elements are stored as dictionary keys and their counts are stored as dictionary values. Counts are allowed to be any integer value including zero or negative counts. The Counter class is similar to bags or multisets in other languages."

这是一个示例代码,可满足您的要求。我使用文件是迭代器的事实来创建Counter对象。迭代时,在文件上它会产生每一行,但不会删除换行符,因此我使用strip()方法获取您建议的输出。

Here is a sample code that does what you are asking for. I used the fact that a file is an iterator to create the Counter object. When you iterate, on a file it yields each line but does not remove the newline character so I used the strip() method to get the output you suggested.

filename = 'test.txt'

filetxt = """\
X_0_Gui_Menu_400_Menu_System
X_0_Gui_Menu_400_Menu_System
X_0_Gui_Menu_000_Menu_root
X_0_Gui_Menu_000_Menu_root
X_0_Gui_Menu_000_Menu_root
X_0_Gui_Menu_300_Menu_Outputs
X_0_Gui_Menu_300_Menu_Outputs
X_0_Gui_Menu_320_Menu_Outputs_SDI
X_0_Gui_Menu_320_Menu_Outputs_SDI
X_0_Gui_Menu_320_Menu_Outputs_SDI
X_0_Gui_Menu_320_Menu_Outputs_SDI
X_0_Gui_Menu_320_Menu_Outputs_SDI
X_0_Gui_Menu_320_Menu_Outputs_SDI
X_0_Gui_Menu_320_Menu_Outputs_SDI
X_0_Gui_Menu_320_Menu_Outputs_SDI
"""

with open(filename, 'w') as f:
    f.write(filetxt)

from collections import Counter
with open(filename, 'r') as f:
    c = Counter(f)

# use iteritems() in python 2.7 instead of items
for key, value in c.items():
    print(key.strip())
    print('{:d} times'.format(value))

这篇关于计算一组字符串出现的次数,并在python 2.7中打印字符串和出现的次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆