Reputation: 123
I am a beginner, and I am trying to find out the number of vowels in each word in a string. So for instance, if I had "Hello there WORLD"
, I want to get an output of [2, 2, 1]
.
Oh, and I am using Python.
I have this so far
[S.count(x) in (S.split()) if x is 'AEIOUaeiou']
where S="Hello there WORLD"
but it keeps saying error. Any hints?
Upvotes: 1
Views: 168
Reputation: 2755
Obviously, S in S.count and S in S.split cannot be the same S. I suggest using more semantic names.
>>> phrase = 'Hello there WORLD'
>>> [sum(letter.casefold() in 'aeiouy' for letter in word) for word in phrase.split()]
[2, 2, 1]
Upvotes: 0
Reputation: 24153
x is 'AEIOUaeiou'
This tests whether x
is precisely the same object as 'AEIOUaeiou'
. This is almost never what you want when you compare objects. e.g. the following could be False
:
>>> a = 'Nikki'
>>> b = 'Nikki'
>>> a is b
False
Although, it may be True
as sometimes Python will optimise identical strings to actually use the same object.
>>> a == b
True
This will always be True
as the values are compared rather than the identity of the objects.
What you probably want is:
x in 'AEIOUaeiou'
Upvotes: 1