12

I am trying to count the occurrences of each letter of a word

word = input("Enter a word")

Alphabet=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']

for i in range(0,26):
    print(word.count(Alphabet[i]))

This currently outputs the number of times each letter occurs including the ones that don't.

How do I list the letters vertically with the frequency alongside it, e.g., like the following?

word="Hello"

H 1

E 1

L 2

O 1

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kelvin San
  • 191
  • 1
  • 1
  • 5
  • 2
    A 30 second search would have revealed you can use `collections.Counter`. – Pythonista Dec 05 '16 at 23:30
  • 3
    This looks like a homework question, so you might want to read [these guidelines](http://meta.stackoverflow.com/a/334823/99127) about how to ask such questions on SO. I'll post a few answers in a moment. – evadeflow Dec 05 '16 at 23:37

13 Answers13

36
from collections import Counter
counts=Counter(word) # Counter({'l': 2, 'H': 1, 'e': 1, 'o': 1})
for i in word:
    print(i,counts[i])

Try using Counter, which will create a dictionary that contains the frequencies of all items in a collection.

Otherwise, you could do a condition on your current code to print only if word.count(Alphabet[i]) is greater than 0, though that would be slower.

LMc
  • 12,577
  • 3
  • 31
  • 43
13
def char_frequency(str1):
    dict = {}
    for n in str1:
        keys = dict.keys()
        if n in keys:
            dict[n] += 1
        else:
            dict[n] = 1
    return dict
print(char_frequency('google.com'))
David
  • 33,444
  • 11
  • 80
  • 118
Uzam Hashmi
  • 143
  • 2
  • 5
  • 10
    Thank you for this code snippet, which might provide some limited short-term help. A proper explanation [would greatly improve](//meta.stackexchange.com/q/114762) its long-term value by showing *why* this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you've made. – Toby Speight Feb 08 '18 at 17:29
  • 1
    Simply iterate through the string and form a key in dictionary of newly occurred element or if element is already occurred, increase its value by 1. – noob_coder Mar 13 '19 at 11:14
  • using dict.keys() jsut to do an `in` test is pointless.. – Tony Suffolk 66 Jan 17 '21 at 17:51
  • Note that you shouldn't name your dictionaries `dict` as that's a reserved word (it's the constructor term for dictionaries!) – duhaime Jan 19 '21 at 00:42
  • ctrl c & ctrl v. from here ---> https://www.w3resource.com/python-exercises/string/python-data-type-string-exercise-2.php – Lord-shiv Apr 15 '21 at 05:08
9

As Pythonista said, this is a job for collections.Counter:

from collections import Counter
print(Counter('cats on wheels'))

This prints:

{'s': 2, ' ': 2, 'e': 2, 't': 1, 'n': 1, 'l': 1, 'a': 1, 'c': 1, 'w': 1, 'h': 1, 'o': 1}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
duhaime
  • 25,611
  • 17
  • 169
  • 224
2
s = input()
t = s.lower()

for i in range(len(s)):
    b = t.count(t[i])
    print("{} -- {}".format(s[i], b))
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Swetank
  • 31
  • 3
2

An easy and simple solution without a library:

string = input()
f = {}
for i in string:
  f[i] = f.get(i,0) + 1
print(f)

Here is the link for get(): https://docs.quantifiedcode.com/python-anti-patterns/correctness/not_using_get_to_return_a_default_value_from_a_dictionary.html

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
soheshdoshi
  • 594
  • 3
  • 7
  • 24
1

Following up what LMc said, your code was already pretty close to functional. You just needed to post-process the result set to remove 'uninteresting' output. Here's one way to make your code work:

#!/usr/bin/env python
word = raw_input("Enter a word: ")

Alphabet = [
    'a','b','c','d','e','f','g','h','i','j','k','l','m',
    'n','o','p','q','r','s','t','u','v','w','x','y','z'
]

hits = [
    (Alphabet[i], word.count(Alphabet[i]))
    for i in range(len(Alphabet))
    if word.count(Alphabet[i])
]

for letter, frequency in hits:
    print letter.upper(), frequency

But the solution using collections.Counter is much more elegant/Pythonic.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
evadeflow
  • 4,704
  • 38
  • 51
1

It might make sense to include all letters of the alphabet. For example, if you're interested in calculating the cosine difference between word distributions you typically require all letters.

You can use this method:

from collections import Counter 

def character_distribution_of_string(pass_string):
  letters = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
  chars_in_string = Counter(pass_string)
  res = {}
  for letter in letters:
    if(letter in chars_in_string):
      res[letter] = chars_in_string[letter]
    else: 
      res[letter] = 0 
  return(res)

Usage:

character_distribution_of_string("This is a string that I want to know about")

Full Character Distribution

{'a': 4,
 'b': 1,
 'c': 0,
 'd': 0,
 'e': 0,
 'f': 0,
 'g': 1,
 'h': 2,
 'i': 3,
 'j': 0,
 'k': 1,
 'l': 0,
 'm': 0,
 'n': 3,
 'o': 3,
 'p': 0,
 'q': 0,
 'r': 1,
 's': 3,
 't': 6,
 'u': 1,
 'v': 0,
 'w': 2,
 'x': 0,
 'y': 0,
 'z': 0}

You can extract the character vector easily:

list(character_distribution_of_string("This is a string that I want to know about").values())

giving...

[4, 1, 0, 0, 0, 0, 1, 2, 3, 0, 1, 0, 0, 3, 3, 0, 0, 1, 3, 6, 1, 0, 2, 0, 0, 0]
Cybernetic
  • 12,628
  • 16
  • 93
  • 132
0

For future references: When you have a list with all the words you want, lets say wordlistit's pretty simple

for numbers in range(len(wordlist)):
    if wordlist[numbers][0] == 'a':
        print(wordlist[numbers])
Stanley
  • 2,434
  • 18
  • 28
0

If using libraries or built-in functions is to be avoided then the following code may help:

s = "aaabbc"  # Sample string
dict_counter = {}  # Empty dict for holding characters
                   # as keys and count as values
for char in s:  # Traversing the whole string
                # character by character
    if not dict_counter or char not in dict_counter.keys(): # Checking whether the dict is
                                                            # empty or contains the character
        dict_counter.update({char: 1}) # If not then adding the
                                       # character to dict with count = 1
    elif char in dict_counter.keys(): # If the character is already
                                      # in the dict then update count
        dict_counter[char] += 1
for key, val in dict_counter.items(): # Looping over each key and
                                      # value pair for printing
    print(key, val)

Output:

a 3
b 2
c 1
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shahriar Rahman Zahin
  • 624
  • 1
  • 11
  • 23
0

Another way could be to remove repeated characters and iterate only on the unique characters (by using set()) and then counting the occurrence of each unique character (by using str.count())

def char_count(string):
    freq = {}
    for char in set(string):
        freq[char] = string.count(char)
    return freq


if __name__ == "__main__":
    s = "HelloWorldHello"
    print(char_count(s))
    # Output: {'e': 2, 'o': 3, 'W': 1, 'r': 1, 'd': 1, 'l': 5, 'H': 2}
Syed Rafay
  • 1,405
  • 2
  • 15
  • 19
0

Initialize an empty dictionary and iterate over every character of the word. If the current character in present in the dictionary, increment its value by 1, and if not, set its value to 1.

word="Hello"
characters={}
for character in word:
    if character in characters:
        characters[character] += 1
    else:
        characters[character] =  1
print(characters)
Saurabh
  • 5,176
  • 4
  • 32
  • 46
0
def string(n):
    a=list()
    n=n.replace(" ","")
    for i in  (n):
        c=n.count(i)
        a.append(i)
        a.append(c)
        y=dict(zip(*[iter(a)]*2))
    print(y)

string("Lets hope for better life")

#Output:{'L': 1, 'e': 5, 't': 3, 's': 1, 'h': 1, 'o': 2, 'p': 1, 'f': 2, 'r': 2, 'b': 1, 'l': 1, 'i': 1} (if u notice in output 2 L-letter one uppercase and other lowercase..if u want them together look for the code below)

In the output, it removes repeated characters, drops empty spaces and iterates only on the unique characters. IF you want to count both uppercase and lowercase together the:

def string(n):
    n=n.lower() #either use (n.uperr()) 
    a=list()
    n=n.replace(" ","")
    for i in  (n):
        c=n.count(i)
        a.append(i)
        a.append(c)
        y=dict(zip(*[iter(a)]*2))
    print(y)

string("Lets hope for better life")

#output:{'l': 2, 'e': 5, 't': 3, 's': 1, 'h': 1, 'o': 2, 'p': 1, 'f': 2, 'r': 2, 'b': 1, 'i': 1}

0
import string
word = input("Enter a word:  ")
word = word.lower()

Alphabet=list(string.ascii_lowercase)
res = []

for i in range(0,26): 
    res.append(word.count(Alphabet[i]))

for i in range (0,26):
    if res[i] != 0:
        print(str(Alphabet[i].upper()) + " " + str(res[i]))
Anaam
  • 124
  • 6