简体   繁体   中英

python lowercase function for-loop

I am trying to create a function to lower case all inputs.

in the end I should be able to lowercase:

result = lowerList('Jen', 'Suzie')
print(result)

so far I have:

def lowerList(input1, input2):
  myList = [input1, input2]
  for x in myList:
    return(x.lower())

result = lowerlist('Jen', 'Suzie')
print(result)

jen

So right now I am only able to get the first input. Also how can I modify this to loop continuously for any number of inputs without modifying the function?

Your problem is that your returning on the first iteration of your for loop, after you've only operated on one element. You need to build up a list of modified values, then return the whole list.

You could do this manually:

def lower_list(*args):
    strings = []
    for string in args:
        strings.append(string.lower())
    return strings

But a simpler and more elegant solution is to use a list comprehension:

def lower_list(*args):
    return [string.lower() for string in args]

Another option if you perfer it, is to use a generator;

def lower_list(*args):
    for string in args:
        yield string.lower()

And it would be used like so:

>>> str_gen = lower_list('Hello', 'WORld', 'Name', 'BUCKs')
>>> next(str_gen)
'hello'
>>> next(str_gen)
'world'
>>> next(str_gen)
'name'
>>> # etc...

Or if you need the entire list of strings at once:

>>> list(lower_list('Hello', 'WORld', 'Name', 'BUCKs'))
['hello', 'world', 'name', 'bucks']
def to_lower(*args):                      
    return [arg.lower() for arg in args]  

>>> to_lower('SPAM', 'Eggs')
['spam', 'eggs']           

You do not return in loop which aborts the for loop right there. Instead create a new list append the lowered elements to it inside the for loop amd return once the loop is done.

That's because when you return the for loop stops. What you should do is append the result string to a list and return it. Using your code as base:

def lowerList(input1, input2):
  myList = [input1, input2]
  l = []
  for x in myList:
    l.append(x.lower())
  return l

result = lowerList('Jen', 'Suzie')
print(result)

Depending on how big your lists are going to get, it may even be more efficient to use generators:

def to_lower(*args):                      
    for arg in args:
        yield arg.lower()  

The reason you are only getting the first input is because you are only returning the first input. After you return from a function, you cannot get back inside that function unless you call it again.

for x in myList: return(x.lower())

Instead, you can do something like this:

def lowerList(myList):
  normalList = []
  for x in myList:
    normalList.append(x.lower())
  return normalList

normalList = ['Jen', 'Suzie']
result = lowerList(normalList)
print(result)

Return a new list where all the items are lowercased, and print that.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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