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)        
  • 相关阅读:
    寒假学习第一天
    课堂测试
    第十六周总结
    第十五周总结
    计算最长单词链
    第十四周总结
    人月神话阅读笔记03
    人月神话阅读笔记02
    第十五周学习进度
    冲刺第二十天
  • 原文地址:https://www.cnblogs.com/songxiaohua/p/9382243.html
Copyright © 2011-2022 走看看