I am currently stuck on trying to output some data and I hope someone can help me.
I made a small example to illustrate what I fail to do:
a = [5, 3, 8, 9]
def function_name(list):
number = 0
for i in list:
number = i + 5
return 'number = %d' % number
Right now my output only is 'number = 14' while I am trying to get the output (below each other): 'n = 10' 'n = 8' etc.
Is there a possibility to do so? I realize that I am overwriting my number variable, but I don't know how else to do this.
The conventional approach to returning multiple values from a function is to create a list, which you append your values to, and which you return after the loop.
a = [5, 3, 8, 9]
def function_name(list):
number = 0
results = []
for i in list:
number = i + 5
results.append('number = %d' % number)
return results
print(function_name(a))
This will give you the output ['number = 10', 'number = 8', 'number = 13', 'number = 14']
. If you want all of these on separate lines, without the quote marks or brackets, you might use str.join
to merge them into one list delimited by newlines.
print("\n".join(function_name(a)))
... Or, replace return results
with:
return "\n".join(results)
Depending on whether it makes sense to perform this operation outside of the function, or inside it. Both work perfectly well, but one will make more sense conceptually depending on the function's purpose.
It is also possible to do this without accumulating a list, by using yield
instead of return. yield
behaves something like return
, except it can be executed multiple times within a function without terminating it. This turns the function into a generator. Fortunately for us, str.join
happily accepts generators as an argument.
a = [5, 3, 8, 9]
def function_name(list):
number = 0
for i in list:
number = i + 5
yield 'number = %d' % number
print("\n".join(function_name(a)))
You need to print inside the loop like so:
def function_name(lst):
number = 0
for i in lst:
number = i + 5
print('number = %d' % number)
a = [5, 3, 8, 9]
function_name(a)
# number = 10
# number = 8
# number = 13
# number = 14
What you do is you return from function at the end of loop, so you are left only with the last value.
return
, you could do:
def function_name(lst): number, lis = 0, [] for i in lst: lis.append(i + 5) return lis a = [5, 3, 8, 9] for number in function_name(a): print('number = %d' % number)
Okay, now you add items to list in the loop and return the list at the end. At where the function is called, we iterate through the returned list and display like previously done.
With the current implementation you are returning the last_element which is 9
and adding 5
to it will return 14
Either loop through the list
a = [5, 3, 8, 9]
def add_5(element):
return element +5
for i in a:
print(add_5(i))
Or return the entire list from function
a = [5, 3, 8, 9]
def add_5_on_list(l):
res=[]
for i in l:
res.append(i+5)
return res
print(add_5_on_list(a))
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.