1# Python program to demonstrate working
2# of map.
3
4# Return double of n
5def addition(n):
6 return n + n
7
8# We double all numbers using map()
9numbers = (1, 2, 3, 4)
10result = map(addition, numbers)
11print(list(result))
1#generate a list from map iterable with lambda expression
2list( map(lambda x: x*2, numeros) )
1# Let's define general python function
2>>> def doubleOrNothing(num):
3... return num * 2
4
5# now use Map on it.
6>>> map(doubleOrNothing, [1,2,3,4,5])
7<map object at 0x7ff5b2bc7d00>
8
9# apply built-in list method on Map Object
10>>> list(map(doubleOrNothing, [1,2,3,4,5])
11[2, 4, 6, 8, 10]
12
13# using lambda function
14>>> list(map(lambda x: x*2, [1,2,3,4,5]))
15[2, 4, 6, 8, 10]
1from multiprocessing import Pool
2
3def test( v ):
4 print(v)
5 return v
6
7if __name__ == '__main__':
8 with Pool(2) as p:
9 print(p.map(test, range(5)))