从列表中返回每个元素(Python)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27261392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:34:39  来源:igfitidea点击:

Returning every element from a list (Python)

pythonreturnreturn-valuemultiple-variable-return

提问by luisamaria

I know that it is possible for a function to return multiple values in Python. What I would like to do is return each element in a list as a separate return value. This could be an arbitrary number of elements, depending on user input. I am wondering if there is a pythonic way of doing so?

我知道一个函数可以在 Python 中返回多个值。我想做的是将列表中的每个元素作为单独的返回值返回。这可以是任意数量的元素,具体取决于用户输入。我想知道是否有这样做的pythonic方式?

For example, I have a function that will return a pair of items as an array, e.g., it will return [a, b].

例如,我有一个函数将一对项目作为数组返回,例如,它将返回[a, b].

However, depending on the input given, the function may produce multiple pairs, which will result in the function returning [[a, b], [c, d], [e, f]]. Instead, I would like it to return [a, b], [c, d], [e, f]

但是,根据给定的输入,函数可能会产生多个对,这将导致函数返回[[a, b], [c, d], [e, f]]。相反,我希望它返回[a, b], [c, d], [e, f]

As of now, I have implemented a very shoddy function with lots of temporary variables and counts, and am looking for a cleaner suggestion.

到目前为止,我已经实现了一个非常粗制滥造的函数,其中包含大量临时变量和计数,并且正在寻找更清晰的建议。

Appreciate the help!

感谢帮助!

采纳答案by wenzul

There is a yieldstatement which matches perfectly for this usecase.

有一个yield语句与这个用例完美匹配。

def foo(a):
    for b in a:
        yield b

This will return a generator which you can iterate.

这将返回一个可以迭代的生成器。

print [b for b in foo([[a, b], [c, d], [e, f]])

回答by matthewatabet

Check out this question: How to return multiple values from *args?

查看这个问题: 如何从 *args 返回多个值?

The important idea is return values, as long as they're a container, can be expanded into individual variables.

重要的思想是返回值,只要它们是一个容器,就可以扩展为单个变量。

回答by akrishnamo

However, depending on the input given, the function may produce multiple pairs, which will result in the function returning [[a, b], [c, d], [e, f]]. Instead, I would like it to return [a, b], [c, d], [e, f]

但是,根据给定的输入,函数可能会产生多个对,这将导致函数返回 [[a, b], [c, d], [e, f]]。相反,我希望它返回 [a, b], [c, d], [e, f]

Can you not just use the returned list (i.e. the list [[a, b], [c, d], [e, f]]) and extract the elements from it? Seems to meet your criteria of arbitrary number of / multiple values.

你不能只使用返回的列表(即列表 [[a, b], [c, d], [e, f]])并从中提取元素吗?似乎满足您的任意数量/多个值的标准。

回答by Lee Daniel Crocker

When a python function executes:

当 python 函数执行时:

return a, b, c

what it actually returns is the tuple (a, b, c), and tuples are unpacked on assignment, so you can say:

它实际返回的是 tuple (a, b, c),并且 tuple 在赋值时被解包,所以你可以说:

x, y, z = f()

and all is well. So if you have a list

一切都很好。所以如果你有一个清单

mylist = [4, "g", [1, 7], 9]

Your function can simply:

您的功能可以简单地:

return tuple(mylist)

and behave like you expect:

并按照您的预期行事:

num1, str1, lst1, num2 = f()

will do the assignments as you expect.

将按照您的预期完成任务。

If what you reallywant is for a function to return an indeterminate number of things as a sequence that you can iterate over, then you'll want to make it a generator using yield, but that's a different ball of wax.

如果您真正想要的是让函数返回不确定数量的事物作为您可以迭代的序列,那么您将希望使用 将它变成一个生成器yield,但这是一个不同的蜡球。