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

如果迭代器為空,Python迭代器中下一個元素的默

Default value for next element in Python iterator if iterator is empty?(如果迭代器為空,Python迭代器中下一個元素的默認值?)
本文介紹了如果迭代器為空,Python迭代器中下一個元素的默認值?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個對象列表,我想找到給定方法為某個輸入值返回 true 的第一個對象.這在 Python 中相對容易做到:

I have a list of objects, and I would like to find the first one for which a given method returns true for some input value. This is relatively easy to do in Python:

pattern = next(p for p in pattern_list if p.method(input))

但是,在我的應用程序中,通常沒有 p.method(input) 為真的這樣的 p,因此這將引發 StopIteration 異常.有沒有一種不寫 try/catch 塊的慣用方法來處理這個問題?

However, in my application it is common that there is no such p for which p.method(input) is true, and so this will raise a StopIteration exception. Is there an idiomatic way to handle this without writing a try/catch block?

特別是,用 if pattern is not None 條件來處理這種情況似乎會更干凈,所以我想知道是否有辦法擴展我對 的定義code>pattern 在迭代器為空時提供 None 值——或者如果有更 Pythonic 的方式來處理整個問題!

In particular, it seems like it would be cleaner to handle that case with something like an if pattern is not None conditional, so I'm wondering if there's a way to expand my definition of pattern to provide a None value when the iterator is empty -- or if there's a more Pythonic way to handle the overall problem!

推薦答案

next 接受默認值:

next accepts a default value:

next(...)
    next(iterator[, default])

    Return the next item from the iterator. If default is given and the iterator
    is exhausted, it is returned instead of raising StopIteration.

等等

>>> print next(i for i in range(10) if i**2 == 9)
3
>>> print next(i for i in range(10) if i**2 == 17)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
>>> print next((i for i in range(10) if i**2 == 17), None)
None

請注意,出于語法原因,您必須將 genexp 包含在額外的括號中,否則:

Note that you have to wrap the genexp in the extra parentheses for syntactic reasons, otherwise:

>>> print next(i for i in range(10) if i**2 == 17, None)
  File "<stdin>", line 1
SyntaxError: Generator expression must be parenthesized if not sole argument

這篇關于如果迭代器為空,Python迭代器中下一個元素的默認值?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 少妇一级淫片aaaaaaaaa | 中文字幕在线视频免费视频 | 亚洲国产精品一区二区三区 | 久久综合伊人一区二区三 | 午夜精品视频在线观看 | 国产成人精品在线播放 | 国产精品福利在线 | 亚洲精品资源 | 亚洲国产精品99久久久久久久久 | av网站免费观看 | 日韩欧美国产精品 | 日本视频一区二区三区 | 国产激情片在线观看 | 超碰日韩 | 国产十日韩十欧美 | 女朋友的闺蜜3韩国三级 | 欧美日韩免费 | 毛片在线看片 | 女女百合av大片一区二区三区九县 | 性做久久久久久免费观看欧美 | 日日干夜夜干 | 中文字幕免费 | 黄色免费av | 国产精品欧美一区二区三区不卡 | 国产精品一区二区三区四区 | 91精品在线播放 | 国产亚洲精品综合一区 | 九九精品在线 | 国产精品一区二区视频 | 伊人久久麻豆 | 欧美专区在线视频 | 天天操天天插 | 一级做a爰片性色毛片16美国 | 手机av网| 做a的各种视频 | 久久久久久国产精品免费免费男同 | 蜜桃av一区二区三区 | 天天操天天玩 | 密室大逃脱第六季大神版在线观看 | 欧美日韩视频在线第一区 | 成人免费看片 |