简体   繁体   English

如何在 function 内的循环内返回值?

[英]How do I return a value within a loop within a function?

I have this almost figured it out but there is one thing.我几乎想通了,但有一件事。 Basically I want to return a string without a vowel (a common challenge I guess).基本上我想返回一个without a vowel的字符串(我猜这是一个常见的挑战)。 This is similar to other challenges on CodeWars I have done, still uncompleted due to this.这与我在 CodeWars 上完成的其他挑战类似,因此仍未完成。 I have a for loop within a function.我在 function 中有一个 for 循环。 I call the function to return value.我调用 function 来返回值。

For some reason, I'm returning empty or rather "None", yet I get the result I wanted by printing.出于某种原因,我返回的是空的,或者更确切地说是“无”,但我通过打印得到了我想要的结果。 On the same line and indenting.在同一行并缩进。

This is for a Codewar challenge, so I need to return values instead of, printing, logging (I know).这是针对 Codewar 挑战的,所以我需要返回值而不是打印、记录(我知道)。 I asked for a friend, hours of researching but nothing could help me.我问了一个朋友,研究了几个小时,但没有什么能帮助我。

def disemvowel(string):
    #aeiou
    vowel = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
    aList = list(string) #'' to [...]

    for x in aList:
      for y in vowel:
        if x == y: 
          #print(x)
          aList.remove(x)
    print(''.join(aList)) # "Ths wbst s fr lsrs LL!"
    return(''.join(aList)) # Nothing shows up here...


I expect the output of "Ths wbst s fr lsrs LL!"我期待"Ths wbst s fr lsrs LL!" by returning but I get None .通过返回但我得到None

https://www.codewars.com/kata/52fba66badcd10859f00097e/train/python Source ^ https://www.codewars.com/kata/52fba66badcd10859f00097e/train/python来源^

To remove vowels from strings, the quickest way would be to use str.replace .要从字符串中删除元音,最快的方法是使用str.replace

def remove_vowels(msg):
    vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
    for vowel in vowels:
        msg = msg.replace(vowel, '')
    return msg

Use a list comprehension:使用列表推导:

def remove_vowels(msg):
    return ''.join(c for c in msg if c.lower() not in {'a', 'e', 'i', 'o', 'u'})

Examples:例子:

>>> remove_vowels("Lorem ipsum dolor sit amet.")
'Lrm psm dlr st mt.'
>> remove_vowels("This is it")
'Ths s t'
>>> remove_vowels("This website is for losers LOL!")
'Ths wbst s fr lsrs LL!'

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

相关问题 我如何在while循环中返回值 - How do I return a value within a while loop 如何将循环的每次迭代中的值返回到其中运行的函数? - How do I return the value from each iteration of a loop to the function it is running within? 如何在 while 循环中退出 for 循环并返回顶部? - How do I exit a for loop within a while loop and return to the top? 如何将类中函数的返回值传递给同一个类中的另一个函数? - How do I pass a return value in a function in a class to another function within the same class? 如何获得在另一个函数中调用的函数以返回值? 蟒蛇 - How do I get a function that is called within another function to return a value? PYTHON 如何在 for 循环中选择“for”循环的第一个值? - how do i select the first value of "for" loop within the for loop? 如何使用Python创建一个函数,该函数将在数据表中的每一行的字典中返回一个值? - How do I create a function that will return a value in a dictionary for each row within a data sheet using Python? 如何从循环内访问range()函数中的参数值? - How do I access the value of a parameter in the range() function from within a loop? 如何在函数内使用for循环返回元素? - How to return elements using for loop within a function? 如何在类中将字典值作为变量返回 - How do I return dictionary value as variable within the class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM