day16 Python map函数

num_l=[1,2,10,5,3,7]
#lambda x:x+1
# def add_one(x):
#     return x+1
#lambda x:x+1
# def reduce_one(x):
#     return x-1
#lambda x:x**2
# def ps(x):
#     return x**2
def map_test(func,array):#func=lambda x:x+1    arrary=[1,2,10,5,3,7]
    ret=[]
    for i in array:
        res = func(i)
        ret.append(res)
    return ret
print(map_test(lambda x:x+1,num_l))
print(map_test(lambda x:x-1,num_l))
print(map_test(lambda x:x**2,num_l))

# for i in res:
#     print(i)
# print(map_test(add_one,num_l))
# print(map_test(reduce_one,num_l))
# print(map_test(ps,num_l))


结果:
[2, 3, 11, 6, 4, 8]
[0, 1, 9, 4, 2, 6]
[1, 4, 100, 25, 9, 49]

猜你喜欢

转载自www.cnblogs.com/charon2/p/10375548.html