[問題] 比對字串中的母音有幾種

作者: 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