Python reduce explanation

后端 未结 5 638
广开言路
广开言路 2020-12-20 21:31

I\'m not able to understand the following code segment:

>>> lot = ((1, 2), (3, 4), (5,))
>>> reduce(lambda t1, t2: t1 + t2, lot)
(1, 2, 3,          


        
5条回答
  •  眼角桃花
    2020-12-20 21:39

    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.
    

提交回复
热议问题