Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何计算文本文件中给定数字的频率_Python - Fatal编程技术网

Python 如何计算文本文件中给定数字的频率

Python 如何计算文本文件中给定数字的频率,python,Python,如何计算文本文件中给定数字的频率。文本文件如下所示 0 2 0 1 0 1 55 100 100 我希望输出如下 0 3 1 2 2 1 55 1 100 2 我试过了,但没有成功 def histogram( A, flAsList=False ): """Return histogram of values

如何计算文本文件中给定数字的频率。文本文件如下所示

     0
     2
     0
     1
     0
     1
     55
     100
     100
我希望输出如下

     0   3
     1   2
     2   1
     55  1
     100 2
我试过了,但没有成功

     def histogram( A, flAsList=False ):
         """Return histogram of values in array A."""
         H = {}
         for val in A:
             H[val] = H.get(val,0) + 1
         if flAsList:
             return H.items()
         return H
还有更好的办法吗。提前谢谢

使用。这是解决这类问题的最佳方法

from collections import Counter
with open('file.txt', 'r') as fd:
    lines = fd.read().split()
    counter = Counter(lines)
    # sorts items
    items = sorted(counter.items(), key=lambda x: int(x[0]))
    # prints desired output
    for k, repetitions in items:
        print k,'\t', repetitions
输出:

0   3
1   2
2   1
55  1
100 2
使用。这是解决这类问题的最佳方法

from collections import Counter
with open('file.txt', 'r') as fd:
    lines = fd.read().split()
    counter = Counter(lines)
    # sorts items
    items = sorted(counter.items(), key=lambda x: int(x[0]))
    # prints desired output
    for k, repetitions in items:
        print k,'\t', repetitions
输出:

0   3
1   2
2   1
55  1
100 2
为此使用对象:

from collections import Counter
c = Counter(A)
现在
c
变量将保存每个值的频率图。例如:

Counter(['a', 'b', 'c', 'a', 'c', 'a'])
=> Counter({'a': 3, 'c': 2, 'b': 1})
为此使用对象:

from collections import Counter
c = Counter(A)
现在
c
变量将保存每个值的频率图。例如:

Counter(['a', 'b', 'c', 'a', 'c', 'a'])
=> Counter({'a': 3, 'c': 2, 'b': 1})

请考虑使用Update:

请考虑使用Update:


使用字典的简单方法:

histogram = {}

with open("file","r") as f:
    for line in f:
        try:
            histogram[line.strip()] +=1
        except KeyError:
            histogram[line.strip()] = 1

for key in sorted(histogram.keys(),key=int):
    print key,"\t",histogram[key]
输出:

0       3
1       2
2       1
55      1
100     2
编辑:

要选择特定列,您需要使用
split()
拆分行。例如,在单个空间上拆分第六个字段:

try:
    histogram[line.strip().split(' ')[5]] +=1
except KeyError:
    histogram[line.strip().split(' ')[5]] = 1

使用字典的简单方法:

histogram = {}

with open("file","r") as f:
    for line in f:
        try:
            histogram[line.strip()] +=1
        except KeyError:
            histogram[line.strip()] = 1

for key in sorted(histogram.keys(),key=int):
    print key,"\t",histogram[key]
输出:

0       3
1       2
2       1
55      1
100     2
编辑:

要选择特定列,您需要使用
split()
拆分行。例如,在单个空间上拆分第六个字段:

try:
    histogram[line.strip().split(' ')[5]] +=1
except KeyError:
    histogram[line.strip().split(' ')[5]] = 1

可能重复的可能重复的感谢sudo_O,很好的一个,因为结果的顺序在增加。如果我需要文件中特定列的此结果,该怎么办?假设第六列给出了数字?谢谢sudo_O,很好,因为结果的顺序是递增的。如果我需要文件中特定列的此结果,该怎么办?假设第六列给出了数字?