繁体   English   中英

计算数字在文件中出现的次数

[英]Counting how many times a number appears in a file

因此,我正在获取文件并通过我的代码运行它。 每行显示在文件中的示例:

100
200
300
100
200
400

我的目标是让我的代码遍历文件中的数字,并且输出是一个字典,该字典以数字为键,并以多少次出现在文件中作为值。 例如:

{100:2,200:2,300:1,400:1}

到目前为止,这是我整理的内容。

def counts(filename):
    d={}
    with open(filename) as f:
        for line in f
            for number in line:


    return d

另外,我可以使用.count()吗? 那么我可以在文件中创建一个数字列表并将其设置为键,然后为该列表提供一个列表,以显示每个数字对应的出现次数并将其设置为键的值吗?

def counts(filename):
    d={}
    with open(filename) as f:
        contents = f.read()

    contents = contents.split("\n")
    del contents[-1]
    contents =  map(int, contents)

    for content in contents:
        if content not in d:
            d[content] = 1
        else:
            d[content] = d[content] + 1

    return d


print counts(filename)

o / p

{200: 2, 300: 1, 400: 1, 100: 2}

您可以创建一个矩阵,其中每个位置可以代表一个数字,其内容代表在文件中显示的数字。 此外,您可以创建一个比较器,与文件中的数字进行比较,然后增加计数器

对于文件中的每个数字,如果它没有在字典中显示为键,则将其添加(计数为0);否则,将其添加为数字。 无论如何,请增加该数字的计数。

这使用生成器读取所有行并将其转换为整数。

from collections import Counter
from csv import reader

def counts(filename):
    return Counter(int(line[0]) for line in reader(open(filename)) if line)


c = counts('my_file.csv')
>>> c
Counter({'100': 2, '200': 2, '300': 1, '400': 1})

>>> c.most_commont(5)
[('200', 2), ('100', 2), ('300', 1), ('400', 1)]

>>> dict(c)
{'100': 2, '200': 2, '300': 1, '400': 1}

一种简单的方法是使用Counter ,一旦完成CounterCounter轻松转换为dict

from collections import Counter 

def counts(filename):
    with open(filename) as f:
        return dict(Counter(int(line) for line in f))

# {200: 2, 100: 2, 300: 1, 400: 1}

我将使用defaultdict来跟踪您的电话号码:

from collections import defaultdict
frequencies = defaultdict(int)
for number in open('numbers.txt'):
    frequencies[int(number)] += 1

for number in sorted(frequencies.keys()):
    print(number, ':', frequencies[number])

给出:

100 : 2
200 : 2
300 : 1
400 : 1

对于常规词典,您需要在第一次遇到数字时捕获KeyError

count = {}
for number in open('numbers.txt'):
    try:
        count[int(number)] += 1
    except KeyError:
        count[int(number)] = 1

仅使用简单的python:

word_count = {}
with open('temp.txt') as file:
    for line in file:
        word_count[line[:-1]] = word_count.setdefault(line[:-1], 0) + 1

如果您想使用精美的库,可以将@alexander的答案与Counter

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM