python中map函数

map函数形参为一个函数和一个迭代对象

给定一个列表,实现加1

L = [1, 2, 3, 4, 5]

res = map(lambda x:x+1, L)

print(list(res))

# 不使用map函数,实现的效果是一模一样的
def add_test(x):
  return x+1

def map_test(func, array):
  temp = []
  for i in array:
    res = func(i)
    temp.add(res)
  return temp

res = map_test(add_test, L)
print(res)

猜你喜欢

转载自www.cnblogs.com/majianyu/p/10087353.html