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

歡迎訪問 生活随笔!

生活随笔

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

python

python mapreduce函数_Map-reduce在Python高阶函数中的应用,python,用法,之,mapreduce

發(fā)布時間:2025/3/19 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python mapreduce函数_Map-reduce在Python高阶函数中的应用,python,用法,之,mapreduce 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

map()

函數(shù)

接收兩個參數(shù),一個是函數(shù),一個是

Iterable

map

將傳入的函數(shù)依次作用到序列的每個元素,并把結(jié)果作為新的

Iterator

返回。

>>> def f(x):

... return x * x

...

>>> r = map(f, [1, 2, 3, 4, 5, 6, 7, 8, 9])

>>> list(r)

[1, 4, 9, 16, 25, 36, 49, 64, 81]

>>> list(map(str, [1, 2, 3, 4, 5, 6, 7, 8, 9]))

['1', '2', '3', '4', '5', '6', '7', '8', '9']

map()函數(shù)能夠簡化編程

reduce()函數(shù)

>>> from functools import reduce

>>> def add(x, y):

... return x + y

...

>>> reduce(add, [1, 3, 5, 7, 9])

25

>>> from functools import reduce

>>> def fn(x, y):

... return x * 10 + y

...

>>> def char2num(s):

... digits = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}

... return digits[s]

...

>>> reduce(fn, map(char2num, '13579'))

13579

利用

map

reduce

編寫一個

str2float

函數(shù),把字符串

'123.456'

轉(zhuǎn)換成浮點數(shù)

123.456

from functools import reduce

def str2float(s):

loc = s.find('.')

s1 = s.replace('.','')

num = reduce(fn, map(char2num, s1))

for n in range(len(s)-loc-1):

num = num/10.0

return num

def char2num(s):

digits = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}

return digits[s]

def fn(x, y):

return x * 10 + y

總結(jié)

以上是生活随笔為你收集整理的python mapreduce函数_Map-reduce在Python高阶函数中的应用,python,用法,之,mapreduce的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。