简体   繁体   中英

Append 2 separate return values of one function to 2 separate lists in a for loop in Python3

I have a function that returns 2 lists separately, in principle like this:

def function_returns_two(many_args**):
    do_something_fancy
    return list1, list2

This function returns a different list1 each time you call it, because there is some random picking of rows from a pandas dataframe involved, while list2 is always the same. — Now I want to iterate this function, in principle:

for i in range(10):
    data, labl = function_returns_two(many_args**)

But obviously that won't work as needed, because for every iteration list1 and list2 get assigned to the same variable ( data , labl ), that is the previously assigned values get replaced.

Instead, I want to append them. — But how? Is it even possible?

for i in range(10):
    (data, labl).append(function_returns_two(many_args**)) # ???

How would you solve this?


PS: Im already using this function for another task, and I'd like to reuse it here. Of course, if what I want is not possible, I can still build a separate function.

This is one approach.

Demo:

data = []
lable = []
for i in range(10):
    d, l = function_returns_two(many_args**)
    data.append(d)
    lable.append(l)

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