1

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?

Dan D.
  • 73,243
  • 15
  • 104
  • 123
python_newbie
  • 113
  • 1
  • 2
  • 8

2 Answers2

1
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'
Peter Wood
  • 23,859
  • 5
  • 60
  • 99
0

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]
Veky
  • 2,646
  • 1
  • 21
  • 30