map函数对可迭代对象里的每个元素执行制定func,返回一个可迭代对象
# define an array
array = [1,2,3,4,5]
# copy operation
ret = []
for i in array:
ret.append(i)
# use a function
def map_test(array):
ret = []
for i in array:
ret.append(i)
return ret
print(map_test(array))
# pass a function into another function
def add_num(x):
return x+1
def map_test(func,array):
ret =[]
for i in array:
ret.append(func(i))
return ret
print(map_test(add_num,array))
# This is equal to the map function built in python
for i in map(lambda x: x+1, range(5)):
print(i)