我是一个初学者,我试图找出字符串中每个单词的元音数量。因此,举例来说,如果我有"Hello there WORLD",我想获得[2, 2, 1]的输出。

哦,我在用Python。

我到目前为止

[S.count(x) in (S.split()) if x is 'AEIOUaeiou']


其中S="Hello there WORLD"

但它总是说错误。有什么提示吗?

最佳答案

显然,S.count中的S和S.split中的S不能是相同的S。我建议使用更多的语义名称。

>>> phrase = 'Hello there WORLD'
>>> [sum(letter.casefold() in 'aeiouy' for letter in word) for word in phrase.split()]
[2, 2, 1]

关于python - 计算字符串中一个单词中的元音数量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32553729/

10-12 00:20
查看更多