python3中内置函数map 和 reduce函数的使用

# map

"""
    map(func, *iterables) --> map object
    
    Make an iterator that computes the function using arguments from
    each of the iterables.  Stops when the shortest iterable is exhausted.
    """

    参数:接收一个函数和一个可迭代对象
    返回map对象,本质是一个迭代器
eg:
    def fun(x):
        return x * x
    map(fun,[1,2,3,4,5])
# 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.
    """
    将两个参数的函数累加应用于序列的项目,从左到右,以便将序列减少到单个值。
     eg:
result = reduce(lambda left, right: pd.merge(left, right, how='left', on=['store_id','item_id']),
                 pd_list)        

猜你喜欢

转载自www.cnblogs.com/songxiaohua/p/9382243.html