Python 高阶函数 传入参数是函数

网友投稿 662 2022-05-30

环境信息

ModelArts

Notebook - pytorch1.4-cuda10.1-cudnn7-ubuntu18.04

JupyterLab - Notebook - Conda-python3

高阶函数 传入参数是函数

def add(num1,num2,fun): return fun(num1)+fun(num2)

add(-1,1,abs) 2

add

abs

my_list = [1,3,5,7] # my_list不变,用fun去处理iterables的每一个元素,并返回一个新的map对象 result = map(lambda x:x*x,my_list) list(result)

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.

相关链接

Python入门篇

Python进阶篇

Python应用篇

帮助文档-AI开发平台ModelArts

Python 高阶函数 传入参数是函数

【生长吧!Python】有奖征文火热进行中

备注

欢迎各位同学一起来交流学习心得^_^

在线课程、沙箱实验、认证、论坛和直播,其中包含了许多优质的内容,推荐了解与学习。

AI开发平台ModelArts Python

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:《TypeScript实战指南》—1.2 准备环境
下一篇:西南科技大学城市学院-城南校区同款FPS地图by杨晓宇
相关文章