[问题] 比对字串中的元音有几种

楼主: tnzikom (tnzikom)   2017-11-03 16:39:00
目前写了判断元音的code如下
word = input()
count = 0
a = e = i = o = u = 0
for num in word.lower():
if num in 'a':
a = 1
elif num in 'e':
e = 1
elif num in 'i':
i = 1
elif num in 'o':
o = 1
elif num in 'u':
u = 1
count = a + e + i + o + u
print(count)
自己觉得这code看起来很笨......想问是不是有更好的写法,
另外也想问for num in word.lower()这行for的运作是怎么样呢?
知道word.lower()是变小写的意思,
但num in word.lower()的意思看了很久还是不太能理解> <
作者: Django (Cython)   2017-11-03 17:38:00
for num in word.lower(): if num in 'aeiou': count += 1(请自行排版) 另外你本来的写法是错的,aeiou的值没有累加
作者: johnlinvc (阿翔)   2017-11-03 17:47:00
import recount=len(re.findall("[AEIOU]", word,re.I))
作者: NTUGG (GG)   2017-11-03 18:35:00
str.count(....)
作者: Yshuan (倚絃)   2017-11-03 21:22:00
[ch in word.lower() for ch in 'aeiou'].count(True)num in word.lower() #小写的word是否有num变量代表的字母
作者: bibo9901 (function(){})()   2017-11-03 22:44:00
len( set("aeiou") & set(word) )
作者: goldflower (金色小黄花)   2017-11-03 23:27:00
word='ptT is Back' for num in word作用如同for num in ['p','t','t',' ','i','s',' ',......]
作者: lance8537 (小砰砰)   2017-11-05 11:08:00
感觉好leetcode

Links booklink

Contact Us: admin [ a t ] ucptt.com