Simon Ayalew
Simon Ayalew

Reputation: 3

Character count into a dictionary as value of character?

How do you write a code that counts the number of occurrences a character has in a file and puts the data in a dictionary?

For example, If I supply the code to a file, I want it to return each new character as a key and the count as the value.

Let's say inside the file is python is cool

I want the code to return {'p':1, 'y':1, 't':1, 'h':1, 'o':3 ...}

I understand how to count the characters in a string by simply using:

def count_chars(text):
    '''
    (string) -> int
    Returns the length of text.

    Examples:
    >>> count_chars("simon")
    5
    >>> count_chars("")
    0
    '''
    count = 0
    for ch in text:
        count += 1
    return count

However, I'm struggling with how to open the file and then put the data into a dictionary.

Upvotes: 0

Views: 5121

Answers (3)

DMe
DMe

Reputation: 7876

Since you've mentioned that this is for an assignment, I thought I'd show you how this is done more 'directly' using a dictionary / without using Counter, so you actually understand what's going on under the hood.

d = {}    # initialise a dictionary
with open(file_path, 'r') as f:    # open the file, call it 'f'
    for line in f:    # read through each line in the file
        for c in line:    # read through each character in that line
            if c in d:    # check if character is in dictionary
                d[c] += 1     # if it's been seen before, increment counter
            else:
                d[c] = 1    # otherwise, insert it into the dictionary

Upvotes: 4

modelbuilder42
modelbuilder42

Reputation: 304

To meet your project requirements simply convert Counter to dict. From Alex's code:

from collections import Counter
def count_characters(source_file_name):
    with open(source_file_name, 'r') as source_file:
        return dict(Counter(source_file.read()))

Upvotes: 1

Alex Taylor
Alex Taylor

Reputation: 8853

Use the included collections.Counter class. The most_common method even includes a code sample being used to count the characters of a string:

>>> Counter('abracadabra').most_common(3)  
[('a', 5), ('r', 2), ('b', 2)]

Putting that together with opening a file:

from collections import Counter
def count_characters(source_file_name):
    with open(source_file_name, 'r') as source_file:
        return Counter(source_file.read())

Upvotes: 3

Related Questions