最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python中的map、reduce和filter淺析

 更新時(shí)間:2014年04月26日 10:46:29   作者:  
這篇文章主要介紹了Python中的map、reduce和filter,用實(shí)例來理解這3個(gè)函數(shù),需要的朋友可以參考下

1、先看看什么是 iterable 對(duì)象

以內(nèi)置的max函數(shù)為例子,查看其doc:

復(fù)制代碼 代碼如下:

>>> print max.__doc__
max(iterable[, key=func]) -> value
max(a, b, c, ...[, key=func]) -> value

With a single iterable argument, return its largest item.
With two or more arguments, return the largest argument.


在max函數(shù)的第一種形式中,其第一個(gè)參數(shù)是一個(gè) iterable 對(duì)象,既然這樣,那么哪些是 iterable 對(duì)象呢?
復(fù)制代碼 代碼如下:

>>> max('abcx')
>>> 'x'
>>> max('1234')
>>> '4'
>>> max((1,2,3))
>>> 3
>>> max([1,2,4])
>>> 4

我們可以使用yield生成一個(gè)iterable 對(duì)象(也有其他的方式):
復(fù)制代碼 代碼如下:

def my_range(start,end):
    ''' '''
    while start <= end:
        yield start
        start += 1

執(zhí)行下面的代碼:
復(fù)制代碼 代碼如下:

for num in my_range(1, 4):
    print num
print max(my_range(1, 4))

將輸出:
復(fù)制代碼 代碼如下:

1
2
3
4
4


2、map

在http://docs.python.org/2/library/functions.html#map中如此介紹map函數(shù):

復(fù)制代碼 代碼如下:

map(function, iterable, ...)
Apply function to every item of iterable and return a list of 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. If one iterable is shorter than another it is assumed to be extended with None items. If function is None, the identity function is assumed; if there are multiple arguments, map() returns a list consisting of tuples containing the corresponding items from all iterables (a kind of transpose operation). The iterable arguments may be a sequence or any iterable object; the result is always a list.

map函數(shù)使用自定義的function處理iterable中的每一個(gè)元素,將所有的處理結(jié)果以list的形式返回。例如:
復(fù)制代碼 代碼如下:

def func(x):
    ''' '''
    return x*x

print map(func, [1,2,4,8])
print map(func, my_range(1, 4))


運(yùn)行結(jié)果是:
復(fù)制代碼 代碼如下:

[1, 4, 16, 64]
[1, 4, 9, 16]

也可以通過列表推導(dǎo)來實(shí)現(xiàn):
復(fù)制代碼 代碼如下:

print [x*x for x in [1,2,4,8]]

3、reduce

在http://docs.python.org/2/library/functions.html#reduce中如下介紹reduce函數(shù):

復(fù)制代碼 代碼如下:

reduce(function, iterable[, initializer])
Apply function of two arguments cumulatively to the items of iterable, from left to right, so as to reduce the iterable 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 iterable. If the optional initializer is present, it is placed before the items of the iterable in the calculation, and serves as a default when the iterable is empty. If initializer is not given and iterable contains only one item, the first item is returned.

這個(gè)已經(jīng)介紹的很明了,
復(fù)制代碼 代碼如下:
reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])

相當(dāng)于計(jì)算
復(fù)制代碼 代碼如下:

((((1+2)+3)+4)+5)

而:
復(fù)制代碼 代碼如下:

reduce(lambda x, y: x+y, [1, 2, 3, 4, 5],6)

相當(dāng)于計(jì)算
復(fù)制代碼 代碼如下:

(((((6+1)+2)+3)+4)+5)


4、filter

在http://docs.python.org/2/library/functions.html#filter中如下介紹filter函數(shù):

復(fù)制代碼 代碼如下:

filter(function, iterable)
Construct a list 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 iterable is a string or a tuple, the result also has that type; otherwise it is always a list. 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 [item for item in iterable if function(item)] if function is not None and [item for item in iterable if item] if function is None.


參數(shù)function(是函數(shù))用于處理iterable中的每個(gè)元素,如果function處理某元素時(shí)候返回true,那么該元素將作為list的成員而返回。比如,過濾掉字符串中的字符a:
復(fù)制代碼 代碼如下:

def func(x):
    ''' '''
    return x != 'a'

print filter(func, 'awake')


運(yùn)行結(jié)果是:
復(fù)制代碼 代碼如下:

wke

這也可以通過列表推導(dǎo)來實(shí)現(xiàn):
復(fù)制代碼 代碼如下:

print ''.join([x for x in 'awake' if x != 'a'])

相關(guān)文章

  • 在Django的上下文中設(shè)置變量的方法

    在Django的上下文中設(shè)置變量的方法

    這篇文章主要介紹了在Django的上下文中設(shè)置變量的方法,Django是重多Python高人氣框架中最為著名的一個(gè),需要的朋友可以參考下
    2015-07-07
  • 在Python中將元組轉(zhuǎn)換為列表的方法詳解

    在Python中將元組轉(zhuǎn)換為列表的方法詳解

    這兩種Python 數(shù)據(jù)類型看起來很相似,但在上下文中卻有不同的用法,元組和列表之間的主要區(qū)別在于它們的可變性,僅當(dāng)您需要修改元素時(shí)才會(huì)將元組轉(zhuǎn)換為列表,本文現(xiàn)在我們將深入研究將元組轉(zhuǎn)換為列表的不同方法,需要的朋友可以參考下
    2023-09-09
  • Python3進(jìn)制之間的轉(zhuǎn)換代碼實(shí)例

    Python3進(jìn)制之間的轉(zhuǎn)換代碼實(shí)例

    這篇文章主要介紹了Python3進(jìn)制之間的轉(zhuǎn)換代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • 通過numba模塊給Python代碼提速的方法詳解

    通過numba模塊給Python代碼提速的方法詳解

    numba是Anaconda公司開發(fā)的針對(duì)Python的開源JIT編譯器,用于提供Python版CPU和GPU編程,速度比原生Python快數(shù)十倍。本文將詳細(xì)介紹一下numba是如何實(shí)現(xiàn)代碼提速的,需要的可以參考一下
    2022-01-01
  • python之class類和方法的用法詳解

    python之class類和方法的用法詳解

    這篇文章主要介紹了python中class類和方法的用法詳解,如果有不太清楚面向?qū)ο蟮念惡头椒ǖ木幊趟枷氲男』锇榭梢越梃b參考本文
    2023-03-03
  • python 默認(rèn)參數(shù)問題的陷阱

    python 默認(rèn)參數(shù)問題的陷阱

    本文給大家講述的是python 默認(rèn)參數(shù)問題的陷阱,有需要的小伙伴可以參考下
    2016-02-02
  • python字符串替換第一個(gè)字符串的方法

    python字符串替換第一個(gè)字符串的方法

    這篇文章主要介紹了python字符串替換第一個(gè)字符串的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-06-06
  • PyTorch模型容器與AlexNet構(gòu)建示例詳解

    PyTorch模型容器與AlexNet構(gòu)建示例詳解

    這篇文章主要為大家介紹了PyTorch模型容器與AlexNet構(gòu)建示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • openCV入門學(xué)習(xí)基礎(chǔ)教程第二篇

    openCV入門學(xué)習(xí)基礎(chǔ)教程第二篇

    人臉識(shí)別,物體檢測(cè),OpenCV是基石,下面這篇文章主要給大家介紹了關(guān)于openCV入門學(xué)習(xí)基礎(chǔ)教程的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-11-11
  • pandas處理csv文件的方法步驟

    pandas處理csv文件的方法步驟

    這篇文章主要介紹了pandas處理csv文件的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10

最新評(píng)論

东山县| 文登市| 上高县| 古浪县| 庆城县| 玉环县| 白朗县| 阿鲁科尔沁旗| 河北省| 开平市| 武安市| 明水县| 金川县| 芒康县| 通化县| 伊宁县| 徐州市| 屏南县| 宜阳县| 西华县| 保定市| 阿克| 虎林市| 砚山县| 松桃| 望城县| 犍为县| 东安县| 庄河市| 司法| 北安市| 农安县| 英德市| 罗田县| 茌平县| 屯门区| 库尔勒市| 深水埗区| 嘉鱼县| 孟连| 洛扎县|