Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 调用返回列表的函数的生成器表达式_Python_Generator Expression - Fatal编程技术网

Python 调用返回列表的函数的生成器表达式

Python 调用返回列表的函数的生成器表达式,python,generator-expression,Python,Generator Expression,我有一个返回列表的函数,我想在使用压缩生成器表达式(或任何好的压缩表达式)调用列表时合并该函数的输出 假设我有一个def foo(bar):其中bar是整数,经过一些疯狂的复杂计算后返回一个列表 foo(1)=[9,1,5] foo(2)=[1] foo(3)=[7,1] arr=[1,2,3] 在一行代码之后,我如何获得arr=[9,1,5,1,7,1] arr=[foo(x)代表arr中的x]给了我[[9,1,5],[1],[7,1]我不想再写一行在列表中列出列表 from iterto

我有一个返回列表的函数,我想在使用压缩生成器表达式(或任何好的压缩表达式)调用列表时合并该函数的输出

假设我有一个
def foo(bar):
其中bar是整数,经过一些疯狂的复杂计算后返回一个列表

foo(1)=[9,1,5]
foo(2)=[1]
foo(3)=[7,1]

arr=[1,2,3]
在一行代码之后,我如何获得
arr=[9,1,5,1,7,1]

arr=[foo(x)代表arr中的x]
给了我
[[9,1,5],[1],[7,1]

我不想再写一行在列表中列出列表

from itertools import chain
result = list(chain.from_iterable(foo(x) for x in arr))