python基础--内置函数map

num_1=[1,2,10,5,3,7]

# num_2=[]
# for i in num_1:
#     num_2.append(i**2)
# print(num_2)


# def map_test(array):
#     num_2=[]
#     for i in num_1:
#         num_2.append(i**2)
#     return num_2
#
# ret=map_test(num_1)
# print(ret)


num_1=[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 pf(x):
    return x**2



#最终结果
def map_test(func,array):#num_1=[1,2,10,5,3,7]
    num_2=[]
    for i in array:
        res=func(i)#lambda匿名函数
        num_2.append(res)
    return num_2

#print(map_test(add_one,num_1))
print(map_test(lambda x:x+1,num_1))
# print(map_test(lambda x:x-1,num_1))
# print(map_test(lambda x:x**2,num_1))
# print(map_test(reduce_one,num_1))
# print(map_test(pf,num_1))



#map内置函数
num=map(lambda x:x+1,num_1)#传的是匿名函数,map函数会默认将可迭代参数进行迭代操作,然后对每个参数使用前面的函数进行操作,最后返回一个map可迭代对象
print(type(num))
# for i in num:
#     print(i)

print(list(num))

#传的是自定义函数
print(list(map(reduce_one,num_1)))#参数一是函数表达式  参数2是可迭代对象

msg="linhaifeng"
mea=list(map(lambda x:x.upper(),msg))
print(mea)

猜你喜欢

转载自www.cnblogs.com/tangcode/p/10984140.html