简体   繁体   English

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

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

So I'm taking a file and running it through my code. 因此,我正在获取文件并通过我的代码运行它。 Example of what appears on file per line: 每行显示在文件中的示例:

100
200
300
100
200
400

My goal is to get my code to iterate through the numbers in the file and the output is a dictionary with the number as the key and how ever many times it appears in the file as the value. 我的目标是让我的代码遍历文件中的数字,并且输出是一个字典,该字典以数字为键,并以多少次出现在文件中作为值。 For example: 例如:

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

This is what I've put together so far. 到目前为止,这是我整理的内容。

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


    return d

Also,would I be able to use .count() for this? 另外,我可以使用.count()吗? So could I create a list of numbers in the file and set those as the keys and then have a list for the set the corresponding amount of times each number appears and set that as the values to the keys? 那么我可以在文件中创建一个数字列表并将其设置为键,然后为该列表提供一个列表,以显示每个数字对应的出现次数并将其设置为键的值吗?

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 o / p

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

u can create a matrix where each position could represent a number and its content represent the number it appears in the file. 您可以创建一个矩阵,其中每个位置可以代表一个数字,其内容代表在文件中显示的数字。 furthermore, u can create a comparator that compares with the numbers from the file and then increase the counter 此外,您可以创建一个比较器,与文件中的数字进行比较,然后增加计数器

For each number in the file, if it does not appear as a key in your dictionary, add it (with a count of 0); 对于文件中的每个数字,如果它没有在字典中显示为键,则将其添加(计数为0);否则,将其添加为数字。 regardless, increment the count for that number. 无论如何,请增加该数字的计数。

This uses a generator to read all of the lines and converts them to integers. 这使用生成器读取所有行并将其转换为整数。

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}

One simple way is to use a Counter , which is easily convertible to a dict once the counting is done. 一种简单的方法是使用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}

I would use a defaultdict to keep track of your number counts: 我将使用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])

Gives: 给出:

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

With a regular dictionary, you need to catch the KeyError on the first time the number is encountered: 对于常规词典,您需要在第一次遇到数字时捕获KeyError

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

Using only simple python: 仅使用简单的python:

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

If you wish to use fancy libraries, you can go with @alexander's answer with Counter . 如果您想使用精美的库,可以将@alexander的答案与Counter

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

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