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

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

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

問題描述

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

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

這個函數可以這樣使用:

This function could be used something like this:

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

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

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:

如果您希望在未找到匹配元素的情況下引發 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)

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

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.

我看到大多數答案堅決忽略next 內置的,所以我假設出于某種神秘的原因,他們 100% 專注于 2.5 及更早版本 - 沒有提及 Python 版本問題(但我沒有在答案中看到 確實提到了內置的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() 如果迭代器立即完成,則迭代器的方法會立即引發 StopIteration —— 即,對于您的用例,如果迭代器中沒有項目滿足條件.如果您不關心(即,您知道 必須 至少有一個令人滿意的項目),那么只需使用 .next() (最好在 genexp 上,行next 在 Python 2.6 及更高版本中內置).

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 中首先指出的那樣將東西包裝在一個函數中,雖然您提出的函數實現很好,但您也可以使用 itertools,一個 for...: break 循環,或一個 genexp,或一個 try/except StopIteration 作為函數的主體,正如各種答案所建議的那樣.這些替代方案中的任何一個都沒有太多附加值,所以我會選擇您最初提出的極其簡單的版本.

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.

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

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

相關文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 久久亚洲国产 | 精品久久精品 | 一区二区三区四区在线视频 | 国产一级毛片精品完整视频版 | 日韩欧美在线一区二区 | 精品1区| 国产免费一级片 | 亚洲国产精品久久久久 | 亚洲经典一区 | 亚洲一区二区免费看 | 久久久久国产精品一区二区 | 中国人pornoxxx麻豆 | 亚洲欧美网 | 国产午夜精品久久久久 | 亚洲美女视频 | 伊人超碰| 精品欧美视频 | 欧美成人一级视频 | 在线视频成人 | 亚洲欧洲av在线 | 97精品国产 | 蜜桃精品噜噜噜成人av | 免费av播放| 久久成人精品视频 | 免费av直接看 | 久久精品国内 | www亚洲免费国内精品 | 日韩一区不卡 | a在线免费观看视频 | 福利片在线观看 | 亚洲一区二区三区视频 | 在线色网站 | 91久久精品视频 | 亚洲国产中文字幕 | 日韩精品久久一区二区三区 | 午夜久久久 | 日韩日b视频 | 欧州一区二区三区 | 国产精品一区二区视频 | 精品亚洲第一 | 成人超碰在线 |