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

歡迎訪問 生活随笔!

生活随笔

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

python

Python函数式编程中map()、reduce()和filter()函数的用法

發布時間:2024/1/17 python 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python函数式编程中map()、reduce()和filter()函数的用法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Python中map()、reduce()和filter()三個函數均是應用于序列的內置函數,分別對序列進行遍歷、遞歸計算以及過濾操作。這三個內置函數在實際使用過程中常常和“行內函數”lambda函數聯合使用,我們首先介紹下lambda函數。

1、lambda函數

lambda函數的Python3.x API文檔

lambda
An anonymous inline function consisting of a single expression which is evaluated when the function is called. The syntax to create a lambda function is lambda [arguments]: expression

由文檔可知,lambda函數是匿名行內函數,其語法為lambda [arguments]: expression,比如:

f = lambda x, y : x * y #定義了函數f(x, y) = x * y

其非匿名函數表達如下:

def f(x, y): return x * y

2、map()函數

map()函數的Python3.x API文檔

map(function, iterable, ...)
Return an iterator that applies function to every item of iterable, yielding the results. If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. With multiple iterables, the iterator stops when the shortest iterable is exhausted. For cases where the function inputs are already arranged into argument tuples, see itertools.starmap().

map()函數的輸入是一個函數function以及一個或多個可迭代的集合iterable,在Python 2.x中map()函數的輸出是一個集合,Python 3.x中輸出的是一個迭代器。map()函數主要功能為對iterable中的每個元素都進行function函數操作,并將所有的返回結果放到集合或迭代器中。function如果是None,則其作用等同于zip()。

例如:

>>> a = map(lambda x, y : x * y, range(3), range(3)) >>> b = list(a) >>> print(b) [0, 1, 4]

在Python 2.x中則不需要?b = list(a),因為在Python 2.x中map()函數的輸出直接就是一個集合。

map()函數的具體執行過程圖如圖1所示。

圖 1?map()函數的具體執行過程圖

由圖1可看出,使用map函數時,兩個可迭代的集合中的元素可以并行進行計算。

對于兩個可迭代的集合的元素個數不一致的情況,map()函數會自動截斷更長的那個集合,并只計算兩個集合對應的元素,比如:

>>> a = map(lambda x, y : x * y, range(3), range(2)) >>> b = list(a) >>> print(b) [0, 1]

3、reduce()函數

reduce()函數的Python3.x API文檔

functools.reduce(function, iterable[, initializer])
Apply function of two arguments cumulatively to the items of sequence, from left to right, so as to reduce the sequence to a single value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). The left argument, x, is the accumulated value and the right argument, y, is the update value from the sequence. If the optional initializer is present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty. If initializer is not given and sequence contains only one item, the first item is returned.

reduce()函數的輸入是一個函數function、一個可迭代的集合iterable以及一個可選的初始項initializer,輸出為一個值。不同于map()函數對序列中的元素進行逐一遍歷,reduce()函數對序列中的元素進行遞歸計算。比如:

>>> from functools import reduce >>> a = reduce(lambda x, y : x * y, [1, 2, 3]) >>> print(a) 6

在 Python3 中,reduce()?函數已經被從全局名字空間里移除了,它現在被放置在?functools模塊里,如果想要使用它,則需要通過引入?functools?模塊來調用?reduce()?函數。

reduce()?函數的具體執行過程圖如圖2所示。

圖2?reduce()?函數的具體執行過程圖

由圖2可以看出,reduce()函數先將可迭代集合中的前兩個元素進行function操作運算,然后將運算結果與第三個元素再進行function操作運算,以此類推,直到迭代完集合中所有的元素,最終返回遞歸結果。

4、filter()函數

filter()函數的Python3.x API文檔

filter(function, iterable)
Construct an iterator from those elements of iterable for which function returns true. iterable may be either a sequence, a container which supports iteration, or an iterator. If function is None, the identity function is assumed, that is, all elements of iterable that are false are removed. Note that filter(function, iterable) is equivalent to the generator expression (item for item in iterable if function(item)) if function is not Noneand (item for item in iterable if item) if function is None.
See itertools.filterfalse() for the complementary function that returns elements of iterable for which function returns false.

filter()函數的輸入為一個函數function和一個可迭代的集合iterable,在Python 2.x中filter()函數的輸出是一個集合,Python 3.x中輸出的是一個filter類。顧名思義,filter()函數主要是對指定可迭代集合進行過濾,篩選出集合中符合條件的元素。比如:

>>> a = filter(lambda x: x > 3 and x < 6, range(7)) >>> print(a) <filter object at 0x108bf2390> >>> b = list(a) >>> print(b) [4, 5]

5、map()、reduce()和filter()與for

在Python的函數式編程中的map()、reduce()和filter()函數,均可用for循環來實現,那么為什么還需要map()、reduce()和filter()函數呢?

主要是因為Python的for命令效率不高且復雜,而map()、reduce()和filter()更為高效和簡潔,map()、reduce()和filter()的循環速度比Python內置的for或while循環要快得多,其執行速度相當于C語言。

def demo_for():x = [x for x in range(100000)] y = [y for y in range(100000)] result = [] for i in range(100000): result.append(x[i] + y[i]) return result def demo_map(): a = map(lambda x, y: x + y, range(100000), range(100000)) return list(a)

在以上的十萬個元素的對比計算中,demo_map的執行效率比demo_for的高2倍之多。廈門叉車修理公司

轉載于:https://www.cnblogs.com/xyou/p/9086703.html

總結

以上是生活随笔為你收集整理的Python函数式编程中map()、reduce()和filter()函数的用法的全部內容,希望文章能夠幫你解決所遇到的問題。

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