Python 高階函數 傳入參數是函數
環境信息
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】有獎征文火熱進行中
備注
歡迎各位同學一起來交流學習心得^_^
在線課程、沙箱實驗、認證、論壇和直播,其中包含了許多優質的內容,推薦了解與學習。
AI開發平臺ModelArts Python
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。