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

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

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

問題描述

我想知道 Python 內(nèi)置函數(shù)中沒有 first(iterable) 是否有原因,有點類似于 any(iterable)all(iterable) (它可能藏在某個 stdlib 模塊中,但我在 itertools 中看不到它).first 將執(zhí)行短路生成器評估,從而可以避免不必要的(并且可能是無限數(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')

這樣你可以表達如下內(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],因為生成器不會終止.

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

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

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)

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

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

推薦答案

如果你有一個迭代器,你可以調(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 中沒有 first(iterable) 內(nèi)置函數(shù)?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Troubles while parsing with python very large xml file(使用 python 解析非常大的 xml 文件時出現(xiàn)問題)
Find all nodes by attribute in XML using Python 2(使用 Python 2 在 XML 中按屬性查找所有節(jié)點)
Python - How to parse xml response and store a elements value in a variable?(Python - 如何解析 xml 響應(yīng)并將元素值存儲在變量中?)
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 對象)
主站蜘蛛池模板: 久久精品无码一区二区三区 | 日本午夜视频 | 91成人免费 | 亚洲3级 | 国产超碰人人爽人人做人人爱 | 96国产精品久久久久aⅴ四区 | 国产精品污污视频 | 日本不卡免费新一二三区 | 亚洲第一黄色网 | 国产一区三区视频 | 亚洲欧美综合精品另类天天更新 | 91九色在线观看 | 久久免费小视频 | 本道综合精品 | 欧美福利一区 | 在线中文一区 | 亚洲天堂av在线 | 日本久久福利 | 天天拍天天草 | 91一区二区 | 欧美日韩不卡合集视频 | 久久不射电影网 | 亚洲国产免费 | 99re在线视频 | 亚洲网站在线播放 | 国产在线一| 亚洲精品无| 91麻豆精品国产91久久久久久久久 | 日本不卡一区 | www.xxxx欧美 | 五月婷婷在线播放 | 精品国产一级 | 毛片一级片| 日韩一区二区三区在线观看 | 久久久久久亚洲精品 | 青青操av| 成人性视频免费网站 | 欧美一区二区三区 | 中文字幕加勒比 | 日本不卡免费新一二三区 | 亚洲精品黄色 |