python 高阶函数 lamdad reduce map

#
# def use_filer(l):
#
# # 过滤偶数
# rest = filter(lambda n: n % 2 != 0, l)
# return rest
#
# if __name__ == ‘__main__’:
# l = [1,2,3,4,5,6,7,8,9,10,11]
# rest = use_filer(l)
# # [1, 3, 5, 7, 9, 11]
# print(list(rest))
#
#
#
# def pow_number(l):
# # 返回 数据的立方
# rest_list = []
# for x in l:
# rest_list.append(x * x * x)
# return rest_list
#
# def f(n):
# return n * n * n
#
# def pow_num_use_map(l):
# return map(f,l)
#
#
# def pow_num_use_lambda(l):
# return map(lambda n:n * n * n,l)
#
#
#
# if __name__ == ‘__main__’:
# l = [1,2,3,4,5,6,7,8,9,10]
# rest = pow_number(l)
# # [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
# print (rest)
# print (‘————————–‘)
# rest_map = pow_num_use_map(l)
# # [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
# print (list(rest_map))
# print (‘————————–‘)
# rest_lambda = pow_num_use_lambda(l)
# # [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
# print (list(rest_lambda))

# 求和
def get_sum(l):
rest = 0
for i in l:
rest += i
return rest

def get_sum_use_py(l):
return sum(l)

from functools import reduce
def f(m,n):
return m + n

def get_sum_use_reduce(l):
return reduce(f,l)

def get_sum_use_lamdad(l):
return reduce(lambda m , n : m + n,l)

if __name__ == ‘__main__’:
l = [1,2,4,6,7,8,9]
rest = get_sum(l)
# 37
print (rest)
print(‘——————‘)
rest_py = get_sum_use_py(l)
print (rest_py)
print(‘——————‘)
rest_reduce = get_sum_use_reduce(l)
print (rest_reduce)
print(‘——————‘)
rest_lamdad = get_sum_use_lamdad(l)
print (rest_lamdad)https://www.cnblogs.com/ericblog1992/p/11280860.html

「点点赞赏,手留余香」

    还没有人赞赏,快来当第一个赞赏的人吧!
0 条回复 A 作者 M 管理员
    所有的伟大,都源于一个勇敢的开始!
欢迎您,新朋友,感谢参与互动!欢迎您 {{author}},您在本站有{{commentsCount}}条评论