Python Basics: Built-in function zip,map,filter

Source: Internet
Author: User

One, zip

Zip, is to combine the two lists, if you want to loop 2 list at the same time, you can use the zip, will help you rotate the two list
For example: L1=[1,2,3,4,5]l2=['a','b','C','D','e'] forA, binchZip (l1,l2):Print(A, b)#The result is
1 A
1 A
3 C
3 C
5 e

If the length of the two list is inconsistent, it is based on a small length

For example: L1=[1,2,3,4]l2=['a','b','C','D','e'] forA, binchZip (l1,l2):Print(A, b)#To get the result is
1 A
2 b
3 C
4 D

Second, map

Loop Call function

def my (num):     return  = [1,2,3,4,5,6,7,8,9= List (map (My,lis))#map parameter The first is the function name, the second is the content to loop, the result is a map Object address, so to cast the next print(res)# with the list will eventually call the contents of the LIS loop my function, res=[' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ' , ' 6 ', ' 7 ', ' 8 ', ' 9 ']

defmy (num1,num2):return(num1,num2) Lis= [1,2,3,4,5]lis1=['a','b','C','D']res= List (map (MY,LIS,LIS1))#The first of the map parameter is the function name, the rest is to loop the content, the function has several parameters, it is necessary to write a few variables, the return result is a Map object address, so to use the list to cast under thePrint(RES)#Eventually, the contents of the LIS and Lis1 are looped through the My function, res=[(1, ' a ') (2, ' B ') (3, ' C ') (4, ' d ')]

Third, filter

Filter and map on the surface is the same, is the loop call function, the difference is: filter is filtered, it will be the content of the loop into the function, the result is true corresponding content returned

def even (num):     if num%2==0:        return  True    return= [ 1,2,3,4,5,6,7,8,9= filter (even,lis)print('filter. ', List (res))  #filter is reserved only, returning the data as true. After the LIS loop is dropped into the even function, the data in the LIS that can return true will be printed, so the end result is 2,4,6,8
#如果改程序用map的话, the function returns what it will print and the end result is false,true,false,true .....

Python Basics: Built-in function zip,map,filter

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.