zoukankan      html  css  js  c++  java
  • Python reduce()

    Description

    The reduce(fun,seq) function is used to apply a particular function passed in its argument to all of the list elements mentioned in the sequence passed along.This function is defined in “functools” module.

    Working : 

    • At first step, first two elements of sequence are picked and the result is obtained.
    • Next step is to apply the same function to the previously attained result and the number just succeeding the second element and the result is again stored.
    • This process continues till no more elements are left in the container.
    • The final returned result is returned and printed on console.

    reduce() 函数会对参数序列中元素进行累积。

    函数将一个数据集合(链表,元组等)中的所有数据进行下列操作:用传给 reduce 中的函数 function(有两个参数)先对集合中的第 1、2 个元素进行操作,得到的结果再与第三个数据用 function 函数运算,最后得到一个结果。

    Example

    # python code to demonstrate working of reduce()
      
    # importing functools for reduce()
    import functools
      
    # initializing list
    lis = [ 1 , 3, 5, 6, 2, ]
      
    # using reduce to compute sum of list
    print ("The sum of the list elements is : ",end="")
    print (functools.reduce(lambda a,b : a+b,lis))
      
    # using reduce to compute maximum element from list
    print ("The maximum element of the list is : ",end="")
    print (functools.reduce(lambda a,b : a if a > b else b,lis))
     
     

    Output:

    The sum of the list elements is : 17
    The maximum element of the list is : 6

    Detail

    Using Operator Functions

    reduce() can also be combined with operator functions to achieve the similar functionality as with lambda functions and makes the code more readable.

    # python code to demonstrate working of reduce()
    # using operator functions
      
    # importing functools for reduce()
    import functools
      
    # importing operator for operator functions
    import operator
      
    # initializing list
    lis = [ 1 , 3, 5, 6, 2, ]
      
    # using reduce to compute sum of list
    # using operator functions
    print ("The sum of the list elements is : ",end="")
    print (functools.reduce(operator.add,lis))
      
    # using reduce to compute product
    # using operator functions
    print ("The product of list elements is : ",end="")
    print (functools.reduce(operator.mul,lis))
      
    # using reduce to concatenate string
    print ("The concatenated product is : ",end="")
    print (functools.reduce(operator.add,["holy","fucking","shit"]))

    Output

    The sum of the list elements is : 17
    The product of list elements is : 180
    The concatenated product is : holyfuckingshit

    reduce() vs accumulate() 

    Both reduce() and accumulate() can be used to calculate the summation of a sequence elements. But there are differences in the implementation aspects in both of these.

    • reduce() is defined in “functools” module, accumulate() in “itertools” module.
    • reduce() stores the intermediate result and only returns the final summation value. Whereas, accumulate() returns a iterator containing the intermediate results. The last number of the iterator returned is summation value of the list.
    • reduce(fun,seq) takes function as 1st and sequence as 2nd argument. In contrast accumulate(seq,fun) takes sequence as 1st argument and function as 2nd argument.
    
    
    # python code to demonstrate summation 
    # using reduce() and accumulate()
      
    # importing itertools for accumulate()
    import itertools
      
    # importing functools for reduce()
    import functools
      
    # initializing list 
    lis = [ 1, 3, 4, 10, 4 ]
      
    # priting summation using accumulate()
    print ("The summation of list using accumulate is :",end="")
    print (list(itertools.accumulate(lis,lambda x,y : x+y)))
      
    # priting summation using reduce()
    print ("The summation of list using reduce is :",end="")
    print (functools.reduce(lambda x,y:x+y,lis))

    Output:

    The summation of list using accumulate is :[1, 4, 8, 18, 22]
    The summation of list using reduce is :22


  • 相关阅读:
    em
    How to locate a path?
    云图说 | 揭秘云硬盘高可靠性的秘密,速来围观
    【华为云技术分享】开发团队中的任务没人领取,你头疼吗?
    介绍一种更方便的代理池实现方案
    4行Python代码生成图像验证码
    【华为云技术分享】机器学习(02)——学习资料链接
    【华为云技术分享】华为开发者大会HDC.Cloud带你探索强化学习三大挑战及落地实践
    【华为云技术分享】【一统江湖的大前端(8)】matter.js 经典物理
    华为开发者大会HDC.Cloud硬核技术解读:知识图谱构建流程及方法
  • 原文地址:https://www.cnblogs.com/xxxsans/p/13553134.html
Copyright © 2011-2022 走看看