Lewis Firmin Lewis Firmin - 28 days ago 22
Python Question

How to print the least used vowel in a string?

I want the user to be able to enter a piece of string (I know how to do this)

Then I want Python to count how many times each vowel (aeiou) is used.

After counting how many times each vowel is used, I need the program to return the least used vowel/s which is/are used at least once. If a vowel is not used it should not be returned. In the event of a tie for least used both should be returned.

If no vowels are used, it should print an error code "No vowels were used" (I know how to do this)

For example: If this is how many times the vowels were used in a sentence:

a=4
b=2
c=0
d=0
e=2


It should print "The least USED vowels were b and c, with 2 uses".

Answer

You can:

  1. Remove all non-vowels chars from the string.
  2. Split the string by chars.
  3. use the Counter on the list you got from #2.

import re
from collections import Counter
s = 'asdfwerasdfwaxciduso'
only_vowels = re.sub(r"[^aeiou]", "", s)
c = Counter(list(only_vowels))
c.most_common()[-1]