Description of the functions of map, reduce, zip, and filter built-in functions in python

Source: Internet
Author: User

Description of the functions of map, reduce, zip, and filter built-in functions in python
Map function usage: map (function, sequence) function: executes function (item) in sequence for items in sequence and returns the List of results.

List1 = [1, 2, 3] list2 = [2, 3, 4] print map (lambda x: x + 1, list1) print map (lambda x, y: x + y, list1, list2) running result: [2, 3, 4] [3, 5, 7]

 

2. reduce function usage: reduce (function, sequence) function: Calls function for sequential iteration of items in sequence
   list1 = [1,2,3]   print reduce(lambda x,y:x+y,list1)

 

Running result: 6 3.zip function usage: zip (function, sequence) function: receives any number of (including 0 and 1) sequences as parameters and returns a tuple list.
List1 = [1, 2, 3] list2 = [2, 3, 4] list3 = [3, 4, 5] list4 = [7] print "zip () ---->", zip () print "zip (list1) ---->", zip (list1) print "zip (list1, list2) ---->", zip (list1, list2) print "zip (list1, list2, list3) ----> ", zip (list1, list2, list3) print" zip (list1, list2, list3, list4) ----> ", zip (list1, list2, list3, list4) # matching result of a long list: zip () ----> [] zip (list1) ----> [(1,), (2,), (3,)] zip (list1, list2) ----> [(1, 2), (2, 3), (3, 4)] zip (list1, list2, list3) ----> [(1, 2, 3), (2, 3, 4), (3, 4, 5)] zip (list1, list2, list3, list4) ----> [(1, 2, 3, 7)]

 

4. filter function usage: filter (function, sequence) function: executes function (item) in sequence for items in sequence ), combine items with the execution result of True into a List/string/tuple (depending on the sequence type)
    list1 = [1,2,3]   print filter(lambda x:x>1,list1)

 

Running result: [2, 3]

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.