Python中替换元音字母的字符串?

7

期望结果:

>>> removeVowels('apple')
"ppl"
>>> removeVowels('Apple')
"ppl"
>>> removeVowels('Banana')
'Bnn'

代码(初学者):

def removeVowels(word):
    vowels = ('a', 'e', 'i', 'o', 'u')
    for c in word:
        if c in vowels:
            res = word.replace(c,"")
    return res 

如何同时使用小写和大写字母?


9个回答

11
re.sub('[aeiou]', '', 'Banana', flags=re.I)

1
vowels = re.compile('[aeiou]', flags=re.I) 然后 vowels.sub('','Banana') - steabert

9

以下是使用列表而不是生成器的版本:

def removeVowels(word):
    letters = []            # make an empty list to hold the non-vowels
    for char in word:       # for each character in the word
        if char.lower() not in 'aeiou':    # if the letter is not a vowel
            letters.append(char)           # add it to the list of non-vowels
    return ''.join(letters) # join the list of non-vowels together into a string

您也可以将其写为:

''.join(char for char in word if char.lower() not in 'aeiou')

这段代码与join函数实现相同,不同之处在于每次找到一个非元音字符就将其添加到新字符串中,而不是先将它们添加到列表中再在结尾处进行拼接。

如果您想加快速度,可以将值的字符串转换为set,这样查找每个字符就更快了,并且由于该字符串包含大写字母,因此无需将每个字符转换为小写。

''.join(char for char in word if char not in set('aeiouAEIOU'))

2
为了加快速度,请使用bytes.translate() - jfs
@JFS 对你的答案点赞,那是“最好”的方法,但它并不能帮助原帖作者理解基本循环、in、或字符串方法。 - agf

6

使用 bytes.translate() 方法:

def removeVowels(word, vowels=b'aeiouAEIOU'):
    return word.translate(None, vowels)

例子:

>>> removeVowels('apple')
'ppl'
>>> removeVowels('Apple')
'ppl'
>>> removeVowels('Banana')
'Bnn'

3

@katrielalex的解决方案也可以简化为生成器表达式:

def removeVowels(word):
    VOWELS = ('a', 'e', 'i', 'o', 'u')
    return ''.join(X for X in word if X.lower() not in VOWELS)

2

由于其他答案都完全重写了代码,我想你会喜欢一个稍微修改了你的代码的答案。另外,我保持了简单,因为你是初学者。

顺便说一下:因为你在每个for循环中重新分配resword,所以你只会得到最后一个元音字母被替换的结果。相反,直接在word中替换元音字母。

def removeVowels(word):
    vowels = ('a', 'e', 'i', 'o', 'u')
    for c in word:
        if c.lower() in vowels:
            word = word.replace(c,"")
    return word

说明:我只是将 if c in vowels: 改为了 if c.lower() in vowels: .lower() 会将字符串转换为小写字母。所以它将每个字母都转换为小写,然后检查该字母是否在元音的元组中,如果是,则进行替换。
其他答案也都很好,如果你还不了解这些方法,可以查看它们使用的方式。
希望能对你有所帮助!

很高兴能帮忙。正如其他人所指出的那样,如果您将元组扩展到包括大写元音字母,您的代码将比每个字母转换为小写字母运行得更快。我只是试图坚持您的一般格式并保持简单。如果您喜欢这个答案,请接受它;-) - Hod - Monica's Army
你应该修复代码示例中的缩进,简单明了。 - steabert

2

这里有另一个简单易用的函数,无需使用列表:

def removeVowels(word):
    vowels = 'aeiouAEIOU'
    for vowel in vowels:
        word = word.replace(vowel, '')
    return word

已经有一段时间了,但这是一种易于理解的初学者方法。我建议你理解这个。 - InDaPond

0
def _filterVowels(word):
    for char in word:
        if char.lower() not in 'aeiou':
            yield char

def removeVowels(word):
    return "".join(_filterVowels(word))

我可以解释它,但我认为你最好阅读文档并尝试自己弄清楚!当学习Python时,这是一个非常好的工具,值得拥有。 - Katriel

0

这个怎么样:

import re

def removeVowels(word):
    return re.sub("[aeiouAEIOU]", "", word)

0
def removeVowels(word):
    vowels='aeiou'
    Vowels='AEIOU'
    newWord=''

    for letter in word:
        if letter in vowels or letter in Vowels:
            cap=letter.replace(letter,'')
        else: cap=letter
        newWord+=cap
    return newWord

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接