zoukankan      html  css  js  c++  java
  • 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)        
  • 相关阅读:
    python笔记目录
    Django 的View(视图)系统
    051_Bootstrap 框架
    050_jQuery 事件
    049_jQuery 操作标签
    048_jQuery
    016-递归函数
    047_BOM_DOM
    046_JS
    045_CSS
  • 原文地址:https://www.cnblogs.com/songxiaohua/p/9382243.html
Copyright © 2011-2022 走看看