日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python | Lambda和map()与示例

發布時間:2023/12/1 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python | Lambda和map()与示例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

The map() function is used to apply the function to all items of an iterable collection like list, tuple etc and it returns the list of the result after performing the functionality defined in the applied function.

map()函數用于將函數應用于可迭代集合的所有項目,如list,tuple等,并在執行所應用函數中定義的功能后返回結果列表。

Example:

例:

Given a list of temperatures and we have to convert 1) all the values in Celsius and 2) all the values in Fahrenheit - using map() with lambda.

給定溫度列表,我們必須轉換1)攝氏溫度的所有值和2)華氏度的所有值-使用帶有lambda的map()。

1) Approach 1: Using normal way

1)方法1:使用常規方法

# function definition to convert temp. from c to f def ctof(c):f=9/5*c+32return f# function definition to convert temp. from f to c def ftoc(f):c=5/9*(f-32)return c# list of the values temp=[12,45,6,78,5,26,67] print("Orignal Data : ",temp)# list declration to store temp. in C cel=[] for t in temp:x=ftoc(t)cel.append(x) print("Celcuis Data : ",cel)# list declration to store temp. in F far=[] for t in temp:x=ctof(t)far.append(x) print("Farenhiet Data : ",far)

Output

輸出量

Orignal Data : [12, 45, 6, 78, 5, 26, 67] Celcuis Data : [-11.11111111111111, 7.222222222222222, -14.444444444444445, 25.555555555555557, -15.0, -3.3333333333333335, 19.444444444444446] Farenhiet Data : [53.6, 113.0, 42.8, 172.4, 41.0, 78.80000000000001, 152.60000000000002]

2) Approach 2: Using map() with lambda

2)方法2:將map()與lambda結合使用

# list of the values temp=[12,45,6,78,5,26,67] print("Orignal Data : ",temp)# converting values to cel using map and lambda cel=list(map(lambda f:5/9*(f-32),temp)) print("Celcuis Data : ",cel)# converting values to far using map and lambda far=list(map(lambda c:9/5*c+32,temp)) print("Farenhiet Data : ",far)

Output

輸出量

Orignal Data : [12, 45, 6, 78, 5, 26, 67] Celcuis Data : [-11.11111111111111, 7.222222222222222, -14.444444444444445, 25.555555555555557, -15.0, -3.3333333333333335, 19.444444444444446] Farenhiet Data : [53.6, 113.0, 42.8, 172.4, 41.0, 78.80000000000001, 152.60000000000002]

翻譯自: https://www.includehelp.com/python/lambda-and-map-with-example.aspx

總結

以上是生活随笔為你收集整理的Python | Lambda和map()与示例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。