Python 高阶函数 传入参数是函数
【摘要】 环境信息ModelArtsNotebook - pytorch1.4-cuda10.1-cudnn7-ubuntu18.04JupyterLab - Notebook - Conda-python3 高阶函数 传入参数是函数 自定义函数def add(num1,num2,fun): return fun(num1)+fun(num2)add(-1,1,abs)2add<functi...
环境信息
- ModelArts
- Notebook - pytorch1.4-cuda10.1-cudnn7-ubuntu18.04
- JupyterLab - Notebook - Conda-python3
- Notebook - pytorch1.4-cuda10.1-cudnn7-ubuntu18.04
高阶函数 传入参数是函数
自定义函数
def add(num1,num2,fun):
return fun(num1)+fun(num2)
add(-1,1,abs)
2
add
<function __main__.add(num1, num2, fun)>
abs
<function abs(x, /)>
map 映射
my_list = [1,3,5,7]
# my_list不变,用fun去处理iterables的每一个元素,并返回一个新的map对象
result = map(lambda x:x*x,my_list)
list(result)
reduce 累计
from functools import reduce
my_list = [1,3,5,7]
# reduce简介: fun(fun(fun(x1,x2),x3),x4)
result = reduce(lambda x,y:x*10+y,my_list)
result
1357
help
help(abs)
Help on built-in function abs in module builtins:
abs(x, /)
Return the absolute value of the argument.
help(map)
Help on class map in module builtins:
class map(object)
| map(func, *iterables) --> map object
|
| Make an iterator that computes the function using arguments from
| each of the iterables. Stops when the shortest iterable is exhausted.
|
| Methods defined here:
......
help(reduce)
Help on built-in function reduce in module _functools:
reduce(...)
reduce(function, sequence[, initial]) -> value
Apply a function of two arguments cumulatively to the items of a sequence,
from left to right, so as to reduce the sequence to a single value.
For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
((((1+2)+3)+4)+5). If initial is present, it is placed before the items
of the sequence in the calculation, and serves as a default when the
sequence is empty.
相关链接
备注
- 欢迎各位同学一起来交流学习心得^_^
- 在线课程、沙箱实验、认证、论坛和直播,其中包含了许多优质的内容,推荐了解与学习。
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)