如果字符串多次包含元音,我很难让函数只打印一次元音。这是作业:
编写Python函数vowelList,它接受一个字符串参数并计算并返回所有元音列表 该字符串中的元音。每个元音只需在列表中列出一次,并且不应有 小写和大写元音的区别。例如,函数调用 元音列表(“密西西比”)。 应该返回 ['I'],因为 'i' 是输入字符串中唯一的元音。
我需要的输出是:
['O']
我不断得到的输出是:
['O', 'O']
这就是我所拥有的:
def vowelList(s):
low = s.lower()
L = []
vowels = "aeiou"
for char in low:
if char not in L and char in vowels:
L.append(char.upper())
return L
print(vowelList("book"))
您将大写字母附加到 L 后,但检查其中是否有小写字母。另外,从一开始就将字符串设置为大写会更好。
up = s.upper()
L = []
vowels = "AEIOU"
for char in up:
if char not in L and char in vowels:
L.append(char)