makavelllli
makavelllli

Reputation: 1

printing statement from a dictionary

text = 'hello'
vowels = 'aeiou'


for char in text.lower():
    if char in vowels:




print(minimum_dict)

How can I make it so this program I wrote prints "vowel x occurs y amount of times".

I tried but I can't get it to work properly, the program Is where there is an input of a word and it checks to see the least frequent vowels which occur.

Upvotes: 0

Views: 77

Answers (3)

Maddy
Maddy

Reputation: 2060

You can loop through the dictionary to get the key and value. items returns a tuple pair.

Include the part below in your code to print the desired result:

for key,value in minimum_dict.items():
    print("Vowel ", key, "occurs", value ," times")

minimum_dict.items() returns a list of items which have the key into the dictionary and it's associated value:

value in this case is equivalent to minimum_dict[key].

Upvotes: 1

Moinuddin Quadri
Moinuddin Quadri

Reputation: 48067

Your code can be simplified using collections.defaultdict() as:

>>> from collections import defaultdict
>>> text = 'hello'
>>> vowels = 'aeiou'
>>> vowel_count = defaultdict(int)
>>> for c in text:
...     if c in vowels:
...         vowel_count[c] += 1
...
>>> vowel_count
{'e': 1, 'o': 1}

In case you had to store the count of all characters, this code could be further simplified using collections.Counter() as:

from collections import Counter
Counter(text)

Upvotes: 1

Nicholas Jones
Nicholas Jones

Reputation: 63

for vowel, occurrences in minimum_dict.items():
    print("vowel", vowel, "occurs ", occurrences, "times")

This will loop through your dictionary of the minimally occurring vowels, and for each vowel/occurrence pair will print the string "vowel", the actual vowel, the string "occurs", the number of occurrences, and the string "times".

The print() function takes any number of unnamed parameters and converts them to strings and then writes them to output.

Upvotes: 0

Related Questions