python reduce function

 

 

 

     python reduce built-in functions and map / filter functions like somewhat similar to a traverse of all the elements through iterator function, the only difference is a function that returns reduce the calculation result is a value, and map / filter returns a sequence or iteration , a detailed explanation in the following

About a .reduce function

1. Grammar

from functools import reduce # 导入模块
reduce(function, sequence[, initial])

 

 

Parameter Description:

    function - a function of two parameters, the required parameters;

    sequence - tuple, list, dictionary, string and the like can iteration was required parameters;

    initial - the initial value, optional parameters;

Return Value: returns the calculated result;

 

2. Principle - the last calculated result as the next input calculated

    Reduce working process is: iterative sequence (tuple, list, dictionary, string and the like can iteration thereof), the first two elements of the first parameter to the function, after the function processing, then the results obtained and third element as a result of two parameters passed to the function parameters, and the function obtained by processing elements and the fourth parameter to the function, and so as the two parameters. If you pass the initial value, then the first pass is not a sequence of first and second elements, but the initial value and the first element. After such a sequence of cumulative combined into a single return value;

For example: the reduce (the lambda X, Y: X + Y, [ 1 , 2 , 3 , 4 , 5 ]) is calculated by (((( 1 + 2 ) + 3 ) + 4 ) + 5 ) = 15

 

 

Function uses two .reduce

1.reduce commonly used functions

! # Usr / bin / env Python 
# - * - Coding: UTF- 8 _ * -
 "" "
 @author: Why grief 
@Blog (personal blog address): shuopython.com 
@WeChat Official the Account (micro-channel public number): Ape said Python 
@Github: www.github.com 
 
@file: python_reduce.py 
@time: 2020 / 3 / 6  10 : 25 
 
@Motto: wonderful short step a thousand miles, no small streams into a mighty torrent, a program of life need to accumulate persistently! 
"" "
  
from functools # import the reduce import modules 
 
DEF func1 (X, Y): 
    # the last calculated result as an input for the next calculation 
    Print ( " X =% Dy = Y% * DX % D = " % (X, Y, X * Y))
     return X * Y
 
if __name__ == "__main__":
    list1 = [1,2,3,4,5]
    value = reduce(func1,list1) #等价 1*2*3*4*5 = 120
    print(value)
    print(type(value))

 

 

Output:

x=1 y=2 x*y=2
x=2 y=3 x*y=6
x=6 y=4 x*y=24
x=24 y=5 x*y=120
120
<class 'int'>

 

This function is actually quite simple: the results of the previous calculation as input for the next calculation!

 

2.reduce function with an anonymous function uses

if __name__ == "__main__":
    list1 = [1,2,3,4,5]
    value = reduce(lambda x,y : x*y ,list1) #等价 1*2*3*4*5 = 120
    print(value)
    print(type(value))

 

Output:

120
<class 'int'>

 

 

Function setting the optional initial 3.reduce

from functools import reduce # 导入模块
 
def func1(x,y):
    return x*y
 
if __name__ == "__main__":
    list1 = [1,2,3,4,5]
    value = reduce(func1,list1,50) #等价 50*1*2*3*4*5 = 6000
    print(value)
    print(type(value))

 

Output:

6000
<class 'int'>

 

 

 

 

you may also like:

    1.python format function / print function explained in detail

    2.python local and global variables

    Shallow vs. deep copy 3.python

 

    Reproduced please specify: ape say Python  »  Python the reduce function

 

Technical exchanges, business cooperation please contact bloggers
Scan code or search: ape say python
No public python tutorial
Ape say python
No. sweep the micro-channel public concern

Guess you like

Origin www.cnblogs.com/shuopython/p/12453854.html