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] >>> 已发布链接: 如果有人认为这是重复的其他职位,

我看过很多关于如何用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]
>>>
已发布链接:

如果有人认为这是重复的其他职位,我会删除它,一旦我了解它的工作原理

谢谢。

这相当于:

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
    ,它:
  • 只接受2个参数
  • 返回使用这两个值计算的值
  • 一个iter可编
    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”
    。看起来函数必须有两个参数,谢谢你的解释。