Special functions and expressions filter, map, reduce, lambda in Python

1. filter

官方解释:filter(function or None, sequence) -> list, tuple, or string

Return those items of sequence for which function(item) is true. If function is None, return the items that are true. If sequence is a tuple or string, return the same type, else return a list.

Incoming parameters: function or None, sequence

Function: It will be filtered according to the method defined in the function, and those that return True will remain, and those that return False will be eliminated.

1 #coding=utf-8
2 def findPositiveNum(num):
3     if num > 0:
4         return True
5     else:
6         return False
7     
8 list={2,1,3,-2,0,12,5,-9}
9 print filter(findPositiveNum,list)
View Code

Return result:

[1, 2, 3, 5, 12]

  

2. map

Official explanation:

map(function, sequence[, sequence, ...]) -> list Return a list of the results of applying the function to the items of the argument sequence(s). If more than one sequence is given,  the function is called with an argument list consisting of the  corresponding item of each sequence, substituting None for missing values when not all sequences have the same length. If the function is None, return  a list of the items of the sequence (or a list of tuples if more than one sequence).

Incoming parameters: function or None, sequence

Function: Pass each parameter in the sequence into the function as a parameter for execution, and the returned result is the result of the value of the sequence passed into the function for execution, which is a sequence. If function is None, the return value is the sequence itself.

1 def multiValue(num):
2     return num**2
3 
4 list1={1,3,5,7,9,11}
5 print map(multiValue,list1)
View Code

Return result:

[1, 9, 25, 49, 81, 121]

If function is passed in as None, the return value is

[1, 3, 5, 7, 9, 11]

3. reduce

Official explanation:

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.

Accumulate a function of two arguments over the sequence terms, left to right, to reduce the sequence to a single subsequence example, reducing (λx,y:x+y,[1,2,3,4,5] ) computes (1+2)+3)+4). If initially present, it is placed before the sequence item is computed, and is used as the default value when the sequence is empty.

1 def addNum(num1,num2):
2     return num1+num2
3 
4 list2={1,2,3,4,5,6}
5 print reduce(addNum, list2)

Code explanation: First calculate the value of 1+2, then pass the calculated value into addNum as the first parameter, the second parameter is 3, and so on. The end result is:

21

If the initial value is passed in then:

1 def addNum(num1,num2):
2     return num1+num2
3 
4 list2={1,2,3,4,5,6}
5 #print reduce(addNum, list2)
6 print reduce(addNum, list2,100)

The result is:

121

4.  lambdas can be used to define anonymous functions

1 addnum= lambda x,y:x+y
2 print addnum(1,2)
3 multiNum=lambda x:x**2
4 print multiNum(5)

The result is

3
25

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325064791&siteId=291194637