python map and filter functions

Python know there are several built-in methods, but not much has been used, the recently re-looked at, re-record it.

map () function can be provided in accordance with the specified sequence is mapped, to python3 returns an iterator are used as follows:

def double(x):
    return 2*x

if __name__=="__main__":
    print(map(double,[1,2,3,4,5]))
    print()
    for i in map(double,[1,2,3,4,5]):
        print(i)

  operation result:

F:\dev\python\python.exe F:/pyCharm/L02_Test/L02Interface/L02_Common/try_demo.py
<map object at 0x000002A3D91A3EF0>

2
4
6
8
10

Process finished with exit code 0

  filter () built-in function for filtering a sequence, for filtering ineligible element returns a list of qualified elements, to python3 returns an iterator.

def is_odd(x):
    return x%2==0

if __name__=="__main__":
    print(filter(is_odd,[1,2,3,4,5,6,7,8,9,10]))
    print()
    for i in filter(is_odd,[1,2,3,4,5,6,7,8,9,10]):
        print(i)

  operation result:

F:\dev\python\python.exe F:/pyCharm/L02_Test/L02Interface/L02_Common/try_demo.py
<filter object at 0x000001C75D243FD0>

2
4
6
8
10

Process finished with exit code 0

  

 

 

Guess you like

Origin www.cnblogs.com/linwenbin/p/11960143.html