国产日韩欧美一区二区三区综合,日本黄色免费在线,国产精品麻豆欧美日韩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人預約都是免費的,你也試試吧...

主站蜘蛛池模板: 思南县| 阿巴嘎旗| 陈巴尔虎旗| 临清市| 田林县| 绥江县| 莲花县| 沙洋县| 东源县| 台前县| 徐汇区| 蓬溪县| 海林市| 霸州市| 陆良县| 孙吴县| 恭城| 古交市| 海门市| 全州县| 会泽县| 奎屯市| 房产| 武川县| 河北区| 辽宁省| 新安县| 铁岭县| 南漳县| 明光市| 新疆| 南平市| 巨野县| 大余县| 大理市| 抚州市| 安乡县| 古蔺县| 汉中市| 石景山区| 绥芬河市|