Reputation: 229
Assuming S = "Tea Lemon CoffEE cAke".lower()
{ x:y.count('aeoiu') for x in S.split() for y in 'aeoiu' if y in 'aeoiu' }
the output of this is:
{'cake': 0, 'tea': 0, 'lemon': 0, 'coffee': 0}
Why is it giving me 0s and not the number of vowels in each word? I'm new to python and I would appreciate some hints. Totally not looking for straight answers.
Upvotes: 0
Views: 958
Reputation: 103525
I think you want to do this:
{ x: sum(x.count(y) for y in 'aeoiu') for x in S.split() }
Which outputs:
{'coffee': 3, 'cake': 2, 'lemon': 2, 'tea': 2}
Your code was counting the number of times the string 'aeoiu' occurs in y
, which is always zero. Also note that for y in 'aeoiu' if y in 'aeoiu'
is redundant, you don't need the if
.
Upvotes: 5