python中的扁平化列表
我看过很多关于如何用Python展平列表的帖子。但我始终无法理解这是如何工作的:python中的扁平化列表,python,list,Python,List,我看过很多关于如何用Python展平列表的帖子。但我始终无法理解这是如何工作的:reduce(lambda x,y:x+y,*myList) 请有人解释一下,这是如何工作的: >>> myList = [[[1,2,3],[4,5],[6,7,8,9]]] >>> reduce(lambda x,y:x+y,*myList) [1, 2, 3, 4, 5, 6, 7, 8, 9] >>> 已发布链接: 如果有人认为这是重复的其他职位,
reduce(lambda x,y:x+y,*myList)
请有人解释一下,这是如何工作的:
>>> myList = [[[1,2,3],[4,5],[6,7,8,9]]]
>>> reduce(lambda x,y:x+y,*myList)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>
已发布链接:
如果有人认为这是重复的其他职位,我会删除它,一旦我了解它的工作原理
谢谢。这相当于:
def my_reduce(func, seq, default=None):
it = iter(seq)
# assign either the first item from the iterable to x or the default value
# passed to my_reduce
x = next(it) if default is None else default
#For each item in iterable, update x by appying the function on x and y
for y in it:
x = func(x, y)
return x
...
>>> my_reduce(lambda a, b: a+b, *myList, default=[])
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> my_reduce(lambda a, b: a+b, *myList)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> from operator import add
>>> my_reduce(add, *myList)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> my_reduce(lambda a, b: a+b, ['a', 'b', 'c', 'd'])
'abcd'
reduce
的Docstring有一个很好的解释:
reduce(...)
reduce(function, sequence[, initial]) -> value
Apply a function of two arguments cumulatively to the items of a sequence,
from left to right, so as to reduce the sequence to a single value.
For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
((((1+2)+3)+4)+5). If initial is present, it is placed before the items
of the sequence in the calculation, and serves as a default when the
sequence is empty.
首先,我不知道为什么它被包装在一个数组中,然后被飞溅(
*
)。这将以同样的方式工作:
>>> myList = [[1,2,3],[4,5],[6,7,8,9]]
>>> reduce(lambda x,y:x+y,myList)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
说明:reduce
采用带有两个参数的方法-累加器和元素。它使用每个元素调用该方法,然后将累加器设置为lambda的结果。因此,基本上是将所有内部列表连接在一起
下面是一个逐步的解释:
reduce(...)
reduce(function, sequence[, initial]) -> value
Apply a function of two arguments cumulatively to the items of a sequence,
from left to right, so as to reduce the sequence to a single value.
For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
((((1+2)+3)+4)+5). If initial is present, it is placed before the items
of the sequence in the calculation, and serves as a default when the
sequence is empty.
myList[0]
,即[1,2,3]
[1,2,3]
和[4,5]
调用lambda,它返回分配给累加器的[1,2,3,4,5]
[1,2,3,4,5]
和[6,7,8,9]
调用lambda,它返回[1,2,3,4,5,6,7,8,9]
reduce
返回reduce(f[a,b,c,d])
runs
f(f(f(f(a, b), c), d)
因为f
是lambda x,y:x+y
,这相当于
((a + b) + c) + d
对于列表,a+b
是列表的串联,因此它连接每个列表
这很慢,因为每个步骤都必须从头开始创建一个新的列表。用简单的英语说,
reduce
做的是,它需要两件事:
f
,它:
iter
(例如列表
或str
)reduce
计算f(iter[0],iter[1])
(iter表的前两项)的结果,并跟踪刚刚计算的该值(称之为temp
)reduce
然后计算f(temp,iter[2])
现在跟踪这个新值。这个过程一直持续到iter
中的每个项目都被传递到f
,并返回计算出的最终值
*
将*myList
传递到reduce
函数中的用法是,它接受一个iterable并将其转换为多个参数。这两条线的作用相同:
myFunc(10,12)
myFunc(*[10,12])
对于myList
,您使用的是一个只包含一个列表的列表。因此,将*
放在前面将myList
替换为myList[0]
关于兼容性,请注意,reduce
函数在Python2中工作得非常好,但在Python3中,您必须这样做:
import functools
functools.reduce(some_iterable)
我已经试着查过了,但我希望有人能直接帮我:我的列表中的*
到底在这里做什么?它是如何从本质上消除myList中的外方括号的?@SimonT这被称为好问题。我刚刚尝试了reduce(lambda x,y,z:x+y,*myList)
并得到TypeError:()缺少一个必需的位置参数:“z”
。看起来函数必须有两个参数,谢谢你的解释。