这是我到目前为止尝试过的代码:
s = 'abcabcde'
count = 0
for letter in s:
if letter in 'aeiou':
count += 1
print ('Number of vowels: ' + str(count))
答案 0 :(得分:1)
对于基于循环的显式解决方案,您可以执行以下操作:
seen = set()
s = 'abcabcde'
for c in s:
if c in 'aeiou':
seen.add(c)
print ('Number of vowels: ' + len(seen))
或者更简洁,使用set intersection:
count = len(set('aeiou') & set('abcabcde'))
答案 1 :(得分:0)
vcount = lambda txt:sum(如果我在txt中,我在'aeiou'中为1)
print(vcount('fox'),vcount('吃') 1 3
答案 2 :(得分:-1)
这应该这样做:
vowels = set(['a','e','i','o','u'])
s = 'abcabcde'
set_s = set(s)
print ('Number of vowels: ' + str(len(vowels & set_s)))
给出
2