Is it possible to get the frequency of characters in more then 1 string in Python?

Viewed 82

Im aware that we are able to get the frequency of characters in a string by doing this:

def freq_of_chars(string):
    all_freq = {}
    for i in string:
        if i in all_freq:
            all_freq[i] += 1
        else:
            all_freq[i] = 1
    print("Count of all characters is: " + str(all_freq)) 
    freq_of_chars('Test')

However, if i want to get the frequency of characters in more then 1 string, how do i do it? I tried this but the output shows the frequency count of each word instead of characters.

def freq_of_chars(string1,string2):
    freq = {}
    for i in string1,string2:
        if i in freq:
            freq[i] += 1
        else:
            freq[i] = 1
    print("Count of all characters is: " + str(freq)) 
    freq_of_chars('First','Second')
4 Answers

Add them together:

def freq_of_chars(string1, string2):
    freq = {}
    for i in string1 + string2:
        if i in freq:
            freq[i] += 1
        else:
            freq[i] = 1
    print("Count of all characters is: " + str(freq)) 

But also you could use:

def freq_of_chars(*strings):
    freq = {}
    for i in ''.join(strings):
        if i in freq:
            freq[i] += 1
        else:
            freq[i] = 1
    print("Count of all characters is: " + str(freq)) 

This way you could enter unlimited strings!

Like:

freq_of_chars('First', 'Second', 'Third', 'Fourth', 'this', 'is', 'a', 'string', 'infinity!')

But I suggest Counter from collections:

from collections import Counter
def freq_of_chars(*strings):
    return Counter(''.join(strings))

But why not just:

freq_of_chars = lambda *strings: Counter(''.join(strings))

Or just no function:

freq_of_chars = Counter(''.join(strings))

The simplest way would be to use freq_of_chars(string1+string2)

This seems like a good opportunity to learn about *args. I'll modify your function:

def freq_of_chars(*strings):
    all_strings = "".join(strings)
    all_freq = {}
    for i in all_strings:
        if i in all_freq:
            all_freq[i] += 1
        else:
            all_freq[i] = 1
    print("Count of all characters is: " + str(all_freq))

Here, I've turned your parameter string into *strings, and then added a line which joins all the strings. What *strings does as a parameter is allow you to pass any number of strings to your function. The "".join() simply joins all the strings so that you can iterate over one big string.

Here's an example of how to use the function:

>>> freq_of_chars("hello", "this", "is", "a", "test", "function", "call", "with", "an", "arbitrary", "number", "of", "strings")
Count of all characters is: {'h': 3, 'e': 3, 'l': 4, 'o': 3, 't': 7, 'i': 6, 's': 5, 'a': 5, 'f': 2, 'u': 2, 'n': 5, 'c': 2, 'w': 1, 'r': 5, 'b': 2, 'y': 1, 'm': 1, 'g': 1}

Now, it's a bit tedious to have to write all those strings out, so if you happen to already have your strings all together in a collection, you can simply unpack that collection in the function call:

>>> strings = ["hello", "this", "is", "a", "test", "function", "call", "with", "an", "arbitrary", "number", "of", "strings"]
>>> freq_of_chars(*strings)
Count of all characters is: {'h': 3, 'e': 3, 'l': 4, 'o': 3, 't': 7, 'i': 6, 's': 5, 'a': 5, 'f': 2, 'u': 2, 'n': 5, 'c': 2, 'w': 1, 'r': 5, 'b': 2, 'y': 1, 'm': 1, 'g': 1}

Rather than implementing the logic for counting the chars you could use collections.Counter instead

from collections import Counter

def freq_of_chars(*strings):
    freq = Counter()
    for string in strings:
        freq.update(string)
    return freq
Related