Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
def anti_元音-编解码器(python)_Python_String_Function - Fatal编程技术网

def anti_元音-编解码器(python)

def anti_元音-编解码器(python),python,string,function,Python,String,Function,我对创建反_元音定义有点执着: 定义一个名为anti_元音的函数,该函数以一个字符串text作为输入,并返回删除所有元音的文本 这是我的尝试: def anti_vowel(text): vowels=["a","A","e","E","i","I","o","O","u","U"] text_input=[""] for char in text: text_input.append(char) av = [char for char in te

我对创建反_元音定义有点执着:

定义一个名为anti_元音的函数,该函数以一个字符串text作为输入,并返回删除所有元音的文本

这是我的尝试:

def anti_vowel(text):
    vowels=["a","A","e","E","i","I","o","O","u","U"]
    text_input=[""]
    for char in text:
        text_input.append(char)
    av = [char for char in text_input if char not in vowels]
    return av
我的代码以单独的字符返回输入

这是我得到的错误:

Oops, try again. Your function fails on anti_vowel("Hey look Words!"). It returns "['', 'H', 'y', ' ', 'l', 'k', ' ', 'W', 'r', 'd', 's', '!']" when it should return "Hy lk Wrds!". 
有人能给我指一下正确的方向吗

return ''.join(av)
将从列表中使其返回为字符串

将它从列表中重新转换为字符串。

您正在查找的字符串以iterable(如列表)作为参数,并返回使用对象作为该iterable的每个元素之间的分隔符而形成的字符串。例如:

lst = ['a','b','c','d','e','f','g']

''.join(lst) # 'abcdefg'
'/'.join(lst) # 'a/b/c/d/e/f/g'
'a'.join(lst # 'aabacadaeafag' etc etc...
请注意,有更好的方法来实现您的功能。(在Python2中使用它,iirc是Codecademy在解释器中使用的,它在不同版本之间更改)天生用于从字符串中删除字符

# Python3
def hate_vowels_py3(instring):
    transmap = str.maketrans('', '', 'aeiouAEIOU')
    outstring = instring.translate(transmap)
    return outstring

# Python2
def hate_vowels_py2(instring):
    outstring = instring.translate(None, 'aeiouAEIOU')
    return outstring
使用你的方法,甚至还有一个渐进的改进。你们俩什么时候都可以

  • 不需要把东西整理得井井有条
  • 不需要有超过一个的东西

您应该强烈考虑使用<代码> SET>代码>而不是<代码>列表。第一次构建

需要稍长的时间,但查找速度要快得多。请记住,如果展开该列表comp,您有:

# example string "Hello world!"

chars_to_delete = ["a","e","i","o","u","A","E","I","O","U"]
string = "Hello world!"
output_list = []
char = "H"
can_append_to_list = True
if char == "a": can_append_to_list = False
if char == "e": can_append_to_list = False
if char == "i": can_append_to_list = False
if char == "o": can_append_to_list = False
if char == "u": can_append_to_list = False
if char == "A": can_append_to_list = False
if char == "E": can_append_to_list = False
if char == "I": can_append_to_list = False
if char == "O": can_append_to_list = False
if char == "U": can_append_to_list = False # ten lookups to this list
if can_append_to_list: output_list.append(char)
char = "e"
can_append_to_list = True
# begin again..........
正如你所看到的,这是对该列表的大量查找。取而代之的是使用一个集合,它的查找时间非常快。使用文字声明:

vowels = {'a','e','i','o','u','A','E','I','O','U'} # looks like a dict with only keys
# (also IS a dict with no keys, but shhh that's an implementation detail)
或者,您可以通过使用iter作为参数调用
set()
从任何iterable创建一个

vowels = set('aeiouAEIOU')
您正在寻找一个以iterable(如列表)为参数并返回字符串的函数,该字符串是使用对象作为iterable的每个元素之间的分隔符形成的。例如:

lst = ['a','b','c','d','e','f','g']

''.join(lst) # 'abcdefg'
'/'.join(lst) # 'a/b/c/d/e/f/g'
'a'.join(lst # 'aabacadaeafag' etc etc...
请注意,有更好的方法来实现您的功能。(在Python2中使用它,iirc是Codecademy在解释器中使用的,它在不同版本之间更改)天生用于从字符串中删除字符

# Python3
def hate_vowels_py3(instring):
    transmap = str.maketrans('', '', 'aeiouAEIOU')
    outstring = instring.translate(transmap)
    return outstring

# Python2
def hate_vowels_py2(instring):
    outstring = instring.translate(None, 'aeiouAEIOU')
    return outstring
使用你的方法,甚至还有一个渐进的改进。你们俩什么时候都可以

  • 不需要把东西整理得井井有条
  • 不需要有超过一个的东西

您应该强烈考虑使用<代码> SET>代码>而不是<代码>列表。第一次构建

需要稍长的时间,但查找速度要快得多。请记住,如果展开该列表comp,您有:

# example string "Hello world!"

chars_to_delete = ["a","e","i","o","u","A","E","I","O","U"]
string = "Hello world!"
output_list = []
char = "H"
can_append_to_list = True
if char == "a": can_append_to_list = False
if char == "e": can_append_to_list = False
if char == "i": can_append_to_list = False
if char == "o": can_append_to_list = False
if char == "u": can_append_to_list = False
if char == "A": can_append_to_list = False
if char == "E": can_append_to_list = False
if char == "I": can_append_to_list = False
if char == "O": can_append_to_list = False
if char == "U": can_append_to_list = False # ten lookups to this list
if can_append_to_list: output_list.append(char)
char = "e"
can_append_to_list = True
# begin again..........
正如你所看到的,这是对该列表的大量查找。取而代之的是使用一个集合,它的查找时间非常快。使用文字声明:

vowels = {'a','e','i','o','u','A','E','I','O','U'} # looks like a dict with only keys
# (also IS a dict with no keys, but shhh that's an implementation detail)
或者,您可以通过使用iter作为参数调用
set()
从任何iterable创建一个

vowels = set('aeiouAEIOU')

您正在返回列表。使用join函数可以将列表转换为相应的字符串

尝试:


您将返回列表,而不是返回av。使用join函数可以将列表转换为相应的字符串

尝试:

请考虑以下事项,而不是返回av:

>>> tgt='This is some text with vowels'
>>> vowels='aeiou'
>>> ''.join(e for e in tgt if e.lower() not in vowels)
'Ths s sm txt wth vwls'

或者,正如评论中所指出的,在join中使用实际的列表理解:

考虑:

>>> tgt='This is some text with vowels'
>>> vowels='aeiou'
>>> ''.join(e for e in tgt if e.lower() not in vowels)
'Ths s sm txt wth vwls'

或者,正如评论中所指出的,在join中使用实际的列表理解:

这是我的解决方案:

def anti_vowel(text):
    vowels = 'aeiouAEIOU'
    textlist = []
    for char in text:
        if char in vowels:
            continue
        else: 
            textlist.append(char)      
    return "".join(textlist)
 passage = '''We hold these truths to be self evident,
    that all men are created equal!'''
    def anti_vowel(text):
        vowels = ['a','e','i','o','u','A','E','I','O','U']
        text = list(text)
        i = 0 
        while i < len(text):
            if text[i] in vowels:
                del(text[i])
                print(i)
                i = i
            else:
                print(text[i])
                i += 1
                s = ''.join(text)
        return s

    print(anti_vowel(passage))
这是我的解决方案:

def anti_vowel(text):
    vowels = 'aeiouAEIOU'
    textlist = []
    for char in text:
        if char in vowels:
            continue
        else: 
            textlist.append(char)      
    return "".join(textlist)
 passage = '''We hold these truths to be self evident,
    that all men are created equal!'''
    def anti_vowel(text):
        vowels = ['a','e','i','o','u','A','E','I','O','U']
        text = list(text)
        i = 0 
        while i < len(text):
            if text[i] in vowels:
                del(text[i])
                print(i)
                i = i
            else:
                print(text[i])
                i += 1
                s = ''.join(text)
        return s

    print(anti_vowel(passage))
这是我的解决方案:

def anti_vowel(text):
    vowels = 'aeiouAEIOU'
    textlist = []
    for char in text:
        if char in vowels:
            continue
        else: 
            textlist.append(char)      
    return "".join(textlist)
 passage = '''We hold these truths to be self evident,
    that all men are created equal!'''
    def anti_vowel(text):
        vowels = ['a','e','i','o','u','A','E','I','O','U']
        text = list(text)
        i = 0 
        while i < len(text):
            if text[i] in vowels:
                del(text[i])
                print(i)
                i = i
            else:
                print(text[i])
                i += 1
                s = ''.join(text)
        return s

    print(anti_vowel(passage))
我们认为这些真理是不言而喻的, 人人生而平等 def anti_元音(文本): 元音=['a'、'e'、'i'、'o'、'u'、'a'、'e'、'i'、'o'、'u'] text=列表(text) i=0 而我这是我的解决方案:

def anti_vowel(text):
    vowels = 'aeiouAEIOU'
    textlist = []
    for char in text:
        if char in vowels:
            continue
        else: 
            textlist.append(char)      
    return "".join(textlist)
 passage = '''We hold these truths to be self evident,
    that all men are created equal!'''
    def anti_vowel(text):
        vowels = ['a','e','i','o','u','A','E','I','O','U']
        text = list(text)
        i = 0 
        while i < len(text):
            if text[i] in vowels:
                del(text[i])
                print(i)
                i = i
            else:
                print(text[i])
                i += 1
                s = ''.join(text)
        return s

    print(anti_vowel(passage))
我们认为这些真理是不言而喻的, 人人生而平等 def anti_元音(文本): 元音=['a'、'e'、'i'、'o'、'u'、'a'、'e'、'i'、'o'、'u'] text=列表(text) i=0 而我解决此问题的另一种简单方法…

def anti_vowel (text):
   new_text=""
   for char in text:
     if char not in "aeiouAEIOU":
       new_text+=char
   return new_text

解决此问题的另一种简单方法…

def anti_vowel (text):
   new_text=""
   for char in text:
     if char not in "aeiouAEIOU":
       new_text+=char
   return new_text

谢谢,这是一个如此简单的解决方案谢谢,这是一个如此简单的解决方案好的速度差没有明显的改善,但这是雷蒙德所以改变了;-)嗯,那里的速度差并没有明显的改善,但这是雷蒙德的改变;-)请回答问题--“有人能为我指出改进代码的正确方向吗”--而不是发布同样有效的代码。请回答问题--“有人能为我指出改进代码的正确方向吗”--而不是发布同样有效的代码。