Python - 将多个值从函数返回到不同的数组

时间:2022-03-24 01:05:13

I have a function where I am returning two values. I would like to put the two values directly into two different arrays. I know how to return the output as two different values to be later added to the array, but I don't want to have the temporary place holders. An example is shown below.

我有一个函数,我返回两个值。我想将这两个值直接放入两个不同的数组中。我知道如何将输出作为两个不同的值返回以便稍后添加到数组中,但我不想拥有临时占位符。一个例子如下所示。

def two_outputs():
    output_one = 5
    output_two = 6
    return output_one, output_two

one_array = []        # initialize array
two_array = []        # initialize array

a, b = two_outputs()  # get values

one_array.append(a)   # store first value in first array
two_array.append(b)   # store second value in first array

Ideally I would like to not use a and b and have to append at a later on in the code. I would like to append the output of the function directly to the two arrays. Is this even possible?

理想情况下,我不想使用a和b,并且必须稍后在代码中附加。我想将函数的输出直接附加到两个数组。这有可能吗?

Thanks for any help. I hope I did this correctly as this is my first post. You guys have helped me quite a bit with programming issues already.

谢谢你的帮助。我希望我这样做是正确的,因为这是我的第一篇文章。你们已经对编程问题给了我很多帮助。

UPDATE: I guess based on the responses below that it is not possible to do this directly. Thanks for everyone's help in finding other ways to accomplish the goal.

更新:我想根据下面的回答,不可能直接这样做。感谢大家帮助找到实现目标的其他方法。

4 个解决方案

#1


5  

How about using a helper function?

如何使用辅助函数?

def zippend(lists, values):
  assert len(lists) == len(values)
  for l,v in zip(lists, values):
    l.append(v)

zippend((one_array, two_array), two_outputs())

The function zippend takes two parameters. The first is an iterable of Lists (what you are referring to as "arrays" are actually Lists in python). The second is an iterable of values to be appended to those lists.

函数zippend有两个参数。第一个是可迭代的列表(你所指的“数组”实际上是python中的列表)。第二个是要附加到这些列表的可迭代值。

It can take as many lists and values as you want, as long as the number of lists matches the number of values (one value per list).

只要列表数与值的数量匹配(每个列表一个值),它就可以使用任意数量的列表和值。

EDIT: If two_outputs() were to return a tuple of Lists to be concatenated onto one_array and two_array, then you could change the function to use extend instead of append:

编辑:如果two_outputs()返回一个列表元组连接到one_array和two_array,那么你可以改变函数使用extend而不是append:

def zextend(lists, values):
  assert len(lists) == len(values)
  for l,v in zip(lists, values):
    l.extend(v)

Or, if you really wanted to, you could use a single function that had an if-statement that checked what kind of values it was getting and appended or extended as appropriate.

或者,如果你真的想要,你可以使用一个if-statement的函数来检查它获取的值,并在适当时追加或扩展。

#2


1  

Assuming I understand you correctly, you would need to define your arrays prior to the declaration of the function.

假设我理解正确,您需要在声明函数之前定义数组。

one_array, two_array = [], []

def two_outputs():
    one_array.append(5)
    two_array.append(6)

#call function
two_outputs()

print one_array, two_array
#[5] [6]

#3


1  

You could always alter your function to return a tuple of lists:

您可以随时更改函数以返回列表元组:

def test():
   # some code
   return [a], [b]

a, b = test()

that will make both a and b lists when they're returned

它们会在返回时生成a和b列表

#4


0  

Can be done in one line with the following generator. The call to "any" is just so that the generator is consumed, and therefore the expressions in it get executed.

可以使用以下生成器在一行中完成。对“any”的调用只是为了消耗生成器,因此其中的表达式被执行。

any(lst.append(item) for lst,item in zip((one_array, two_array), two_outputs()))

NB. I do not recomend this programing style - it gets harder to read. Probably, if it where too frequent an idiom, I'd write a short helper function for the assignment like:

NB。我不推荐这种编程风格 - 它越来越难以阅读。也许,如果它是一个过于频繁的习语,我会为作业编写一个简短的辅助函数,如:

def multi_append(lists, multi_function, *args, **kw):
    for lst, result in zip(lists, multi_function(*args, **kw)):
         lst.append(result)

And on the "body" of the code, just write:

在代码的“正文”上,只需写:

multi_append((array_one, array_two), two_outputs)

For the sake of completeness, I am adding a suggestion that would allow you to use the assignment operator.

为了完整起见,我添加了一个允许您使用赋值运算符的建议。

What is needed in this case, is a custom List object that has a property wich performs the appending. Creating such a class is a 2 liner, but then, he lists in your code would have to be from this class:

在这种情况下需要的是一个自定义List对象,它具有执行追加的属性。创建这样一个类是一个2班轮,但是,他在你的代码中列出必须来自这个类:

class MList(list):
    last = property(lambda s:s[-1], list.append)

array_one, array_two = MList(), MList()

array_one.last, array_two.last = two_outputs()

#1


5  

How about using a helper function?

如何使用辅助函数?

def zippend(lists, values):
  assert len(lists) == len(values)
  for l,v in zip(lists, values):
    l.append(v)

zippend((one_array, two_array), two_outputs())

The function zippend takes two parameters. The first is an iterable of Lists (what you are referring to as "arrays" are actually Lists in python). The second is an iterable of values to be appended to those lists.

函数zippend有两个参数。第一个是可迭代的列表(你所指的“数组”实际上是python中的列表)。第二个是要附加到这些列表的可迭代值。

It can take as many lists and values as you want, as long as the number of lists matches the number of values (one value per list).

只要列表数与值的数量匹配(每个列表一个值),它就可以使用任意数量的列表和值。

EDIT: If two_outputs() were to return a tuple of Lists to be concatenated onto one_array and two_array, then you could change the function to use extend instead of append:

编辑:如果two_outputs()返回一个列表元组连接到one_array和two_array,那么你可以改变函数使用extend而不是append:

def zextend(lists, values):
  assert len(lists) == len(values)
  for l,v in zip(lists, values):
    l.extend(v)

Or, if you really wanted to, you could use a single function that had an if-statement that checked what kind of values it was getting and appended or extended as appropriate.

或者,如果你真的想要,你可以使用一个if-statement的函数来检查它获取的值,并在适当时追加或扩展。

#2


1  

Assuming I understand you correctly, you would need to define your arrays prior to the declaration of the function.

假设我理解正确,您需要在声明函数之前定义数组。

one_array, two_array = [], []

def two_outputs():
    one_array.append(5)
    two_array.append(6)

#call function
two_outputs()

print one_array, two_array
#[5] [6]

#3


1  

You could always alter your function to return a tuple of lists:

您可以随时更改函数以返回列表元组:

def test():
   # some code
   return [a], [b]

a, b = test()

that will make both a and b lists when they're returned

它们会在返回时生成a和b列表

#4


0  

Can be done in one line with the following generator. The call to "any" is just so that the generator is consumed, and therefore the expressions in it get executed.

可以使用以下生成器在一行中完成。对“any”的调用只是为了消耗生成器,因此其中的表达式被执行。

any(lst.append(item) for lst,item in zip((one_array, two_array), two_outputs()))

NB. I do not recomend this programing style - it gets harder to read. Probably, if it where too frequent an idiom, I'd write a short helper function for the assignment like:

NB。我不推荐这种编程风格 - 它越来越难以阅读。也许,如果它是一个过于频繁的习语,我会为作业编写一个简短的辅助函数,如:

def multi_append(lists, multi_function, *args, **kw):
    for lst, result in zip(lists, multi_function(*args, **kw)):
         lst.append(result)

And on the "body" of the code, just write:

在代码的“正文”上,只需写:

multi_append((array_one, array_two), two_outputs)

For the sake of completeness, I am adding a suggestion that would allow you to use the assignment operator.

为了完整起见,我添加了一个允许您使用赋值运算符的建议。

What is needed in this case, is a custom List object that has a property wich performs the appending. Creating such a class is a 2 liner, but then, he lists in your code would have to be from this class:

在这种情况下需要的是一个自定义List对象,它具有执行追加的属性。创建这样一个类是一个2班轮,但是,他在你的代码中列出必须来自这个类:

class MList(list):
    last = property(lambda s:s[-1], list.append)

array_one, array_two = MList(), MList()

array_one.last, array_two.last = two_outputs()