How to stop the Counter function printing the actual world 'counter'?


I am trying to create a piece of coding which counts how many times each vowel is used in a sentence. My current coding looks like:

sentence = str(input("Please enter your line of text; ").lower())
only_vowels = re.sub(r"[^aeiou]", "", sentence)

c = (collections.Counter(list(only_vowels)))


However, when I print enter the sentence as "Hello World"....and the coding tries to print 'c', it returns...

Counter({'o': 2, 'e': 1})

Of course, it has done its job and counted how many times each vowel occurred... However, I do not want the word 'Counter' to be printed at the front each time. Is there any way I can stop this?!

Show source
| function   | python   | printing   | counter   2016-11-12 16:11 2 Answers

Answers ( 2 )

  1. 2016-11-12 16:11
    >>> c = dict(Counter({'o':2, 'e': 1}))
    >>> c
    {'o': 2, 'e': 1}
  2. 2016-11-12 16:11

    It's printing "Counter" because that's what the Counter object is designed to do when you request it in string form. what you want is the dictionary inside of counter. To do this, you want to convert it to a dict object. This can be done by doing:

    print dict(Counter({'o':2, 'e': 1}))
◀ Go back