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

從匹配條件的迭代中獲取第一個項目

Get the first item from an iterable that matches a condition(從匹配條件的迭代中獲取第一個項目)
本文介紹了從匹配條件的迭代中獲取第一個項目的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我想從符合條件的列表中獲取第一項.重要的是,生成的方法不處理整個列表,這可能非常大.例如,下面的函數(shù)就足夠了:

I would like to get the first item from a list matching a condition. It's important that the resulting method not process the entire list, which could be quite large. For example, the following function is adequate:

def first(the_iterable, condition = lambda x: True):
    for i in the_iterable:
        if condition(i):
            return i

這個函數(shù)可以這樣使用:

This function could be used something like this:

>>> first(range(10))
0
>>> first(range(10), lambda i: i > 3)
4

但是,我想不出一個好的內(nèi)置/單線讓我這樣做.如果不需要的話,我并不特別想復(fù)制這個功能.是否有內(nèi)置方法可以獲取第一個匹配條件的項目?

However, I can't think of a good built-in / one-liner to let me do this. I don't particularly want to copy this function around if I don't have to. Is there a built-in way to get the first item matching a condition?

推薦答案

在 Python 2.6+ 和 Python 3 中:

In Python 2.6+ and Python 3:

如果您希望在未找到匹配元素的情況下引發(fā) StopIteration:

If you want StopIteration to be raised if no matching element is found:

next(x for x in the_iterable if x > 3)

如果您希望返回 default_value(例如 None):

If you want default_value (e.g. None) to be returned instead:

next((x for x in the_iterable if x > 3), default_value)

請注意,在這種情況下,您需要在生成器表達式周圍添加一對額外的括號 - 只要生成器表達式不是唯一的參數(shù),就需要它們.

Note that you need an extra pair of parentheses around the generator expression in this case ? they are needed whenever the generator expression isn't the only argument.

我看到大多數(shù)答案堅決忽略next 內(nèi)置的,所以我假設(shè)出于某種神秘的原因,他們 100% 專注于 2.5 及更早版本 - 沒有提及 Python 版本問題(但我沒有在答案中看到 確實提到了內(nèi)置的next,這就是為什么我認為有必要自己提供一個答案——至少正確的版本"問題會以這種方式記錄下來;-).

I see most answers resolutely ignore the next built-in and so I assume that for some mysterious reason they're 100% focused on versions 2.5 and older -- without mentioning the Python-version issue (but then I don't see that mention in the answers that do mention the next built-in, which is why I thought it necessary to provide an answer myself -- at least the "correct version" issue gets on record this way;-).

在 2.5 中,.next() 如果迭代器立即完成,則迭代器的方法會立即引發(fā) StopIteration —— 即,對于您的用例,如果迭代器中沒有項目滿足條件.如果您不關(guān)心(即,您知道 必須 至少有一個令人滿意的項目),那么只需使用 .next() (最好在 genexp 上,行next 在 Python 2.6 及更高版本中內(nèi)置).

In 2.5, the .next() method of iterators immediately raises StopIteration if the iterator immediately finishes -- i.e., for your use case, if no item in the iterable satisfies the condition. If you don't care (i.e., you know there must be at least one satisfactory item) then just use .next() (best on a genexp, line for the next built-in in Python 2.6 and better).

如果您確實在乎,最好按照您在 Q 中首先指出的那樣將東西包裝在一個函數(shù)中,雖然您提出的函數(shù)實現(xiàn)很好,但您也可以使用 itertools,一個 for...: break 循環(huán),或一個 genexp,或一個 try/except StopIteration 作為函數(shù)的主體,正如各種答案所建議的那樣.這些替代方案中的任何一個都沒有太多附加值,所以我會選擇您最初提出的極其簡單的版本.

If you do care, wrapping things in a function as you had first indicated in your Q seems best, and while the function implementation you proposed is just fine, you could alternatively use itertools, a for...: break loop, or a genexp, or a try/except StopIteration as the function's body, as various answers suggested. There's not much added value in any of these alternatives so I'd go for the starkly-simple version you first proposed.

這篇關(guān)于從匹配條件的迭代中獲取第一個項目的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區(qū)域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應(yīng)用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據(jù)文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 国产精品一区二区视频 | 老头搡老女人毛片视频在线看 | 日韩一区二区在线视频 | 亚洲国产成人av好男人在线观看 | 欧美日韩一二三区 | 欧美性网| 免费国产一区 | 国产欧美一区二区三区另类精品 | 在线天堂免费中文字幕视频 | 精品久久久久久久久久久久久久久久久 | 三级av在线 | 久久99久久99久久 | 91在线观看免费视频 | 天天操夜夜操 | 国产精品日韩一区 | 视频一二三区 | 在线播放91 | 中文字幕 欧美 日韩 | 在线a视频 | 午夜视频一区二区三区 | 免费一级大片 | 亚洲综合首页 | 欧美一级黄色免费看 | 欧美一区二区大片 | 国内久久精品 | 久久免费视频1 | 中文字幕一区二区三区乱码图片 | 日韩精品无码一区二区三区 | 特黄小视频 | 欧美一a | 色综合视频| 亚洲综合一区二区三区 | 亚洲影音先锋 | 欧美日韩大片 | 玖玖免费| 亚洲国产精品一区二区第一页 | 国产精品成人一区二区三区夜夜夜 | 欧美激情视频一区二区三区在线播放 | 欧美日韩亚洲国产综合 | 日本免费网 | 久久精品小视频 |