国产日韩欧美一区二区三区综合,日本黄色免费在线,国产精品麻豆欧美日韩ww,色综合狠狠操

極客小將

您現在的位置是:首頁 » python編程資訊

資訊內容

python中lambda的用法

極客小將2020-12-02-
簡介對于一個函數,只有一句話表示,那么就可以用lambda表達式表示,如:def f(x):return x * xprint(f(5))out: 25可以寫為:f = lambda x: x*x #&n

對于一個函數,只有一句話表示,那么就可以用lambda表達式表示,如:VG4少兒編程網-Scratch_Python_教程_免費兒童編程學習平臺

def f(x): return x * x print(f(5))out: 25

可以寫為:VG4少兒編程網-Scratch_Python_教程_免費兒童編程學習平臺

f = lambda x: x*x # 冒號左邊為輸入,右邊是返回值,f是函數名 print(f(5))out: 25

對于多個形式參數:VG4少兒編程網-Scratch_Python_教程_免費兒童編程學習平臺

g = lambda x,y: x+y # 冒號左邊為輸入,右邊是返回值,f是函數名 print(g(4,5))out: 9

lambda用到比較多的地方是排序,如:VG4少兒編程網-Scratch_Python_教程_免費兒童編程學習平臺

def get_four(my): return my[2] tuple_my = [] file = open("file.csv", "r") for line in file: Line = line.strip() arr = line.split(",") one = arr[1] three = arr[3] four = int(arr[4]) tuple_my.append( (one, three, four) ) tuple_my.sort(key=get_four) for my in tuple_my: print(my)

可以寫為:VG4少兒編程網-Scratch_Python_教程_免費兒童編程學習平臺

get_four = lambda my: my[2] tuple_my = [] file = open("file.csv", "r") for line in file: Line = line.strip() arr = line.split(",") one = arr[1] three = arr[3] four = int(arr[4]) tuple_my.append( (one, three, four) ) tuple_my.sort(key=get_four) for my in tuple_my: print(my)tuple_my = [] file = open("file.csv", "r") for line in file: Line = line.strip() arr = line.split(",") one = arr[1] three = arr[3] four = int(arr[4]) tuple_my.append( (one, three, four) ) tuple_my.sort(key=lambda my: my[2]) for my in tuple_my: print(my)

lambda也經常用在符合函數下,如:VG4少兒編程網-Scratch_Python_教程_免費兒童編程學習平臺

def quadratic(a, b, c): return lambda x: a*x*x*x + b*x*x + c*x f = quadratic(3, -2, 4) print(f(5))345def quadratic(a, b, c): return lambda x: a*x*x*x + b*x*x + c*x print(quadratic(3, -2, 4)(5))345

預約試聽課

已有385人預約都是免費的,你也試試吧...

主站蜘蛛池模板: 静宁县| 赤壁市| 隆化县| 江津市| 阿荣旗| 泰兴市| 原平市| 多伦县| 东城区| 房山区| 沭阳县| 龙井市| 阳曲县| 太仓市| 绥中县| 屯留县| 铁岭县| 湘乡市| 隆安县| 溧水县| 汉寿县| 洛隆县| 自贡市| 九台市| 淳安县| 静宁县| 灵丘县| 上虞市| 天等县| 日喀则市| 资溪县| 贵州省| 威宁| 淅川县| 泰和县| 南漳县| 阿拉善右旗| 肇州县| 当阳市| 海宁市| 林甸县|