0

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)))

print(c)

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?!

  • `print` can prints only text so it converts `Counter` object to text before it prints it. If you don't like it then convert `Counter` to text on your own. – furas Nov 12 '16 at 15:18

2 Answers2

1
>>> c = dict(Counter({'o':2, 'e': 1}))
>>> c
{'o': 2, 'e': 1}
>>>
Simeon Aleksov
  • 1,275
  • 1
  • 13
  • 25
1

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}))
Community
  • 1
  • 1
David Garwin
  • 401
  • 3
  • 6