简体   繁体   English

字符串替换Python中的元音?

[英]String replace vowels in Python?

Expected: 预期:

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

Code (Beginner): 代码(初学者):

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

How do I both lowercase and uppercase? 我如何小写和大写?

Here is a version using a list instead of a generator: 这是使用列表而不是生成器的版本:

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

You could also write it just as 你也可以把它写成

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

Which does the same thing, except finding the non-vowels one at a time as join needs them to make the new string, instead of adding them to a list then joining them at the end. 这做同样的事情,只是在寻找一个时间非元音一个join需要他们做出新的字符串,而不是将它们添加到列表,然后在最后加入他们。

If you wanted to speed it up, making the string of values a set makes looking up each character in them faster, and having the upper case letters too means you don't have to convert each character to lowercase. 如果你想加快速度,那么将值字符串set为一set就可以更快地查找其中的每个字符,并且使用大写字母也意味着您不必将每个字符转换为小写字母。

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

Using bytes.translate() method: 使用bytes.translate()方法:

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

Example: 例:

>>> removeVowels('apple')
'ppl'
>>> removeVowels('Apple')
'ppl'
>>> removeVowels('Banana')
'Bnn'
re.sub('[aeiou]', '', 'Banana', flags=re.I)

@katrielalex solution can be also simplified into a generator expression: @katrielalex解决方案也可以简化为生成器表达式:

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

Here you have another simple and easy to use function, no need to use lists: 这里有另一个简单易用的功能,无需使用列表:

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

Since all the other answers completely rewrite the code I figured you'd like one with your code, only slightly modified. 由于所有其他答案都完全重写了代码,我认为你想要一个代码,只需稍加修改。 Also, I kept it simple, since you're a beginner. 另外,我保持简单,因为你是初学者。

A side note: because you reassign res to word in every for loop, you'll only get the last vowel replaced. 旁注:因为你在每个for循环中重新分配resword ,你只会替换最后一个元音。 Instead replace the vowels directly in word 而是直接用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

Explanation: I just changed if c in vowels: to if c.lower() in vowels: 说明:我只是改变if c in vowels: if c.lower() in vowels: if c in vowels: if c.lower() in vowels:

.lower() convert a string to lowercase. .lower()将字符串转换为小写。 So it converted each letter to lowercase, then checked the letter to see if it was in the tuple of vowels, and then replaced it if it was. 因此它将每个字母转换为小写,然后检查字母以查看它是否在元音元组中,然后如果它是替换它。

The other answers are all good ones, so you should check out the methods they use if you don't know them yet. 其他答案都很好,所以如果你还不知道它们,你应该查看它们使用的方法。

Hope this helps! 希望这可以帮助!

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
def _filterVowels(word):
    for char in word:
        if char.lower() not in 'aeiou':
            yield char

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

how about this: 这个怎么样:

import re

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM