简体   繁体   English

当输入包含“Qu”时,.pop function 为什么不从元音列表中删除字母“u”和“U”?

[英]Why doesn't the .pop function remove letters "u" and "U" from the list vowels when the input contains "Qu"?

If the word begins with the letters "qu", the letter "u"/"U" should be removed from the list vowels, however, my code doesn't work.如果单词以字母“qu”开头,则应从元音列表中删除字母“u”/“U”,但是,我的代码不起作用。 How can I fix this?我怎样才能解决这个问题?

For context, this code is for converting English to Pig Latin.对于上下文,此代码用于将英语转换为 Pig Latin。

vowels = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]
qu = ["qu", "Qu"]
def firstVowelIndex(w):
    for index, char in enumerate(w):
        if char in vowels:
            return index


def encryptVow(w):
    w = w + "-way"
    return w

def encryptCon(w):
    wordToList = list(w)
    if wordToList[0:2] in qu:
        vowels.pop(4)
        vowels.pop(9)
        index = firstVowelIndex(w)
        return w[index:] + "-" + w[:index] + 'ay'

    vowels.append("y")
    vowels.append("Y")
    index = firstVowelIndex(w)
    return w[index:] + "-" + w[:index] + 'ay'

def encrypt(w):
    wordToList = list(w)
    if wordToList[0] in vowels:
        return encryptVow(w)

    elif wordToList[0] not in vowels:
            return encryptCon(w)



if __name__ == '__main__':

    print(encrypt("quiz"))

I'd say this should do what you asked in your question.我会说这应该做你在你的问题中提出的问题。

if word.lower().startswith("qu"):
    vowels.remove("U")
    vowels.remove("u")

PS: You do not have to turn a string into a list because a string is an iterable of characters. PS:您不必将字符串转换为列表,因为字符串是可迭代的字符。 As such you can subindex it without needing to change datatype.因此,您可以对其进行子索引而无需更改数据类型。 As such you could have also used something like:因此,您也可以使用类似的东西:

if word[0:2].lower() == "qu":
    vowels.remove("U")
    vowels.remove("u")

But I'd say using the string method startswith() is more explicit.但我会说使用字符串方法startswith()更明确。

Also consider that if you use the same vowels list for multiple words.还要考虑一下,如果您对多个单词使用相同的vowels列表。 The code above will raise a ValueError cause effectively you already removed those vowels before.上面的代码将引发ValueError ,因为您之前已经删除了那些元音。

First, you are using vowels as a global variable.首先,您将vowels用作全局变量。 It is OK since you call encrypt() only once.没关系,因为您只调用了一次encrypt() But if you use it multiple times, once the letters 'u' and 'U' are removed, they will not be present after a word starting with 'q' has been encountered.但是如果多次使用,一旦把'u'和'U'字母去掉,遇到'q'开头的单词后,它们就不会出现了。

Each time the function encrypt() is called, it appends 'y' and 'Y' multiple times.每次调用 function encrypt()时,它都会多次附加“y”和“Y”。

Method pop() cannot be called with a string, it must be called with an integer. Use search() .方法pop()不能用字符串调用,它必须用 integer 调用。使用search()

However, add print(vowels) at the start of the firstVowelIndex() function. You will see how the vowels list changes.但是,在firstVowelIndex() function 的开头添加print(vowels) 。您将看到vowels列表如何变化。

Consult python documentation about global variables.请参阅 python 有关全局变量的文档。

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

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