Special syntax in Python: filter, map, reduce, lambda introduction, pythonlambda

Source: Internet
Author: User

Special syntax in Python: filter, map, reduce, lambda introduction, pythonlambda

Filter (function, sequence ):Execute function (item) in sequence for items in sequence, and combine items with the execution result of True into a List/String/Tuple (depending on the sequence type) to return:
Copy codeThe Code is as follows:
>>> Def f (x): return x % 2! = 0 and x % 3! = 0
>>> Filter (f, range (2, 25 ))
[5, 7, 11, 13, 17, 19, 23]
>>> Def f (x): return x! = 'A'
>>> Filter (f, "abcdef ")
'Bcdef'

Map (function, sequence ):Execute function (item) in sequence for items in sequence. See the execution result to form a List and return:
Copy codeThe Code is as follows:
>>> Def cube (x): return x * x
>>> Map (cube, range (1, 11 ))
[1, 8, 27, 64,125,216,343,512,729,100 0]
>>> Def cube (x): return x + x
...
>>> Map (cube, "abcde ")
['A', 'bb ', 'cc', 'dd', 'ee']

In addition, map also supports multiple sequence, which requires that the function also supports the corresponding number of parameter inputs:
Copy codeThe Code is as follows:
>>> Def add (x, y): return x + y
>>> Map (add, range (8), range (8 ))
[0, 2, 4, 6, 8, 10, 12, 14]

Reduce (function, sequence, starting_value ):Call the function sequentially for items in sequence. If starting_value exists, it can also be called as an initial value. For example, it can be used to sum the List:
Copy codeThe Code is as follows:
>>> Def add (x, y): return x + y
>>> Reduce (add, range (1, 11 ))
55 (Note: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10)
>>> Reduce (add, range (1, 11), 20)
75 (Note: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 20)

Lambda:This is an interesting syntax supported by Python. It allows you to quickly define the smallest function of a single row. Similar to Macros in C, these lambda functions are borrowed from LISP, it can be used in any function:
Copy codeThe Code is as follows:
>>> G = lambda x: x * 2
>>> G (3)
6
>>> (Lambda x: x * 2) (3)
6

We can also combine filter map reduce and lambda to write a simple row of functions.
For example:
Copy codeThe Code is as follows:
Kmpathes = filter (lambda kmpath: kmpath,
Map (lambda kmpath: string. strip (kmpath ),
String. split (l ,':')))

It looks troublesome. In fact, it is as elegant as it is to describe the problem in a language.
Splits all elements in l by ':' To produce a list. Strip each element of the List to form a list. Perform a direct return operation on each element in the list (filter conditions can be added here) to obtain a list separated, each string in the list is strip and can filter special strings.

Related Article

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.