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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

Python高级函数--map/reduce

發布時間:2023/11/27 生活经验 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python高级函数--map/reduce 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

名字開頭大寫 后面小寫;練習:

1 def normalize(name):
2     return name[0].upper() + name[1:].lower()
3 L1 = ['adam', 'LISA', 'barT']
4 L2 = list(map(normalize, L1))
5 print(L2)

reduce求積:

1 from functools import reduce
2 
3 def prod(L):
4     def fn(x, y):
5         return x * y
6     return reduce(fn, L)
7 print('3 * 5 * 7 * 9 =', prod([3, 5, 7, 9]))

reduce把結果繼續和序列的下一個元素做累積計算

字符串轉浮點數練習:

 1 from functools import reduce
 2 
 3 def str2int(s):
 4     def char2num(c):
 5         return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[c]
 6     return reduce(lambda x, y: x *10 + y, map(char2num, s))
 7 
 8 def str2float(s):
 9     s_list = s.split('.')
10     float_i = str2int(s_list[0]) #123
11     float_f = str2int(s_list[1]) / (10**len(s_list[1])) #456/1000
12     return float_i + float_f
13 print('str2float(\'123.456\') =', str2float('123.456'))

?

轉載于:https://www.cnblogs.com/bingbug/p/7819412.html

總結

以上是生活随笔為你收集整理的Python高级函数--map/reduce的全部內容,希望文章能夠幫你解決所遇到的問題。

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