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

Source: Internet
Author: User

1. Filter

Official Explanation: Filter (function or None, sequence), list, tuple, or string

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

Incoming parameter: function or none, sequence

Function: Will filter by the way defined in function, return true to leave, return false culling.

1 #Coding=utf-82 deffindpositivenum (num):3     ifnum >0:4         returnTrue5     Else:6         returnFalse7     8List={2,1,3,-2,0,12,5,-9}9 PrintFilter (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 are given, the function is called with an argument list consisting of the corresponding item of Each of the sequence, substituting None for missing values while not all sequences has 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 parameter: function or none, sequence

Function: Executes the parameters in the sequence in the function as a parameter, the result of which is the value of the sequence passed in the function to execute the result, is a sequence. If function is none, then the return value is the sequence itself.

1 def multivalue (num): 2     return num**234 list1={1,3,5,7,9,11}5print map ( MULTIVALUE,LIST1)
View Code

return Result:

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

If function is passed to none, the return value is

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

3. Reduce

Official explanation:

Reduce (function, sequence[, initial]), value

Apply a function of the 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's placed before the items of the sequence in the calculation, and serves as a default when the Sequence is empty.

The functions of the two parameters are accumulated on the sequence item, left to right, to reduce the sequence to a single subsequence example, (λx,y:x+y,[1,2,3,4,5]) (1+2)) (+3) +4). If it is initially present, it is placed before the calculated sequence item and is left as the default value when the sequence is empty.

1 def Addnum (num1,num2): 2     return num1+num234 list2={1,2,3,4,5,6}5Print Reduce (Addnum, List2)

Code interpretation: Calculate the value of 1+2 first, then pass the computed value as the first parameter into Addnum, the second parameter is 3, and so on. The end result is:

21st

If the incoming initial is worth saying:

1 def Addnum (num1,num2): 2     return num1+num234 list2={1,2,3,4,5,6}5#  Print reduce (addnum, List2)6print reduce (addnum, list2,100)

The result is:

121

4. Lambda can be used to define anonymous functions

1 Lambda x,y:x+y2print addnum (3) multinum=Lambda x : x**24print multinum (5)

Result is

325

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.