久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

為什么 Python 中沒(méi)有 first(iterable) 內(nèi)置函數(shù)?

Why is there no first(iterable) built-in function in Python?(為什么 Python 中沒(méi)有 first(iterable) 內(nèi)置函數(shù)?)
本文介紹了為什么 Python 中沒(méi)有 first(iterable) 內(nèi)置函數(shù)?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我想知道 Python 內(nèi)置函數(shù)中沒(méi)有 first(iterable) 是否有原因,有點(diǎn)類(lèi)似于 any(iterable)all(iterable) (它可能藏在某個(gè) stdlib 模塊中,但我在 itertools 中看不到它).first 將執(zhí)行短路生成器評(píng)估,從而可以避免不必要的(并且可能是無(wú)限數(shù)量的)操作;即

I'm wondering if there's a reason that there's no first(iterable) in the Python built-in functions, somewhat similar to any(iterable) and all(iterable) (it may be tucked in a stdlib module somewhere, but I don't see it in itertools). first would perform a short-circuit generator evaluation so that unnecessary (and a potentially infinite number of) operations can be avoided; i.e.

def identity(item):
    return item

def first(iterable, predicate=identity):
    for item in iterable:
        if predicate(item):
            return item
    raise ValueError('No satisfactory value found')

這樣你可以表達(dá)如下內(nèi)容:

This way you can express things like:

denominators = (2, 3, 4, 5)
lcd = first(i for i in itertools.count(1)
    if all(i % denominators == 0 for denominator in denominators))

顯然你不能在這種情況下執(zhí)行 list(generator)[0],因?yàn)樯善鞑粫?huì)終止.

Clearly you can't do list(generator)[0] in that case, since the generator doesn't terminate.

或者,如果您有一堆正則表達(dá)式要匹配(當(dāng)它們都具有相同的 groupdict 接口時(shí)很有用):

Or if you have a bunch of regexes to match against (useful when they all have the same groupdict interface):

match = first(regex.match(big_text) for regex in regexes)

通過(guò)避免 list(generator)[0] 和在正匹配時(shí)短路,您可以節(jié)省大量不必要的處理.

You save a lot of unnecessary processing by avoiding list(generator)[0] and short-circuiting on a positive match.

推薦答案

如果你有一個(gè)迭代器,你可以調(diào)用它的 next 方法.比如:

If you have an iterator, you can just call its next method. Something like:

In [3]: (5*x for x in xrange(2,4)).next()
Out[3]: 10

這篇關(guān)于為什么 Python 中沒(méi)有 first(iterable) 內(nèi)置函數(shù)?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Troubles while parsing with python very large xml file(使用 python 解析非常大的 xml 文件時(shí)出現(xiàn)問(wèn)題)
Find all nodes by attribute in XML using Python 2(使用 Python 2 在 XML 中按屬性查找所有節(jié)點(diǎn))
Python - How to parse xml response and store a elements value in a variable?(Python - 如何解析 xml 響應(yīng)并將元素值存儲(chǔ)在變量中?)
How to get XML tag value in Python(如何在 Python 中獲取 XML 標(biāo)記值)
How to correctly parse utf-8 xml with ElementTree?(如何使用 ElementTree 正確解析 utf-8 xml?)
Parse XML from URL into python object(將 XML 從 URL 解析為 python 對(duì)象)
主站蜘蛛池模板: 午夜精品 | 久久精品国产清自在天天线 | 中文字幕精| 国产日韩精品一区二区 | 成人三级在线播放 | 99久久99| 天天干视频网 | 国产美女h视频 | 成人性生交大片免费看中文带字幕 | 亚洲综合视频 | 人成在线视频 | 国产精品电影网 | 成人高清在线 | 欧美日韩不卡 | 我要看免费一级毛片 | 日韩欧美二区 | 欧美日韩不卡合集视频 | 欧美日韩久久精品 | 日韩中文欧美 | 久久尤物免费一区二区三区 | 国产二区在线播放 | 特黄毛片| 一区二区三区四区av | 国产一级特黄视频 | 国产亚洲精品精品国产亚洲综合 | 狠狠做六月爱婷婷综合aⅴ 国产精品视频网 | 久久精品国产一区二区电影 | 久久精品久久久久久 | 亚洲精品1 | 日本理论片好看理论片 | 97精品国产97久久久久久免费 | 久久综合一区 | 日日日色 | 日本三级日产三级国产三级 | 日韩一区在线播放 | 日韩毛片在线免费观看 | av电影手机在线看 | 国产精品久久久久久久久久免费 | 在线看91 | 日韩av一区二区在线观看 | 亚洲国产精品成人久久久 |