从匹配条件的迭代器中获取第一个项目的最好方法是什么? [英] What is the best way to get the first item from an iterable matching a condition?

查看:121
本文介绍了从匹配条件的迭代器中获取第一个项目的最好方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中,我想从符合条件的列表中获取第一个项目。例如,以下函数就足够了:

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

此函数可以使用如下:

 >>>第一(范围(10))
0
>>>>第一个(范围(10),lambda i:i> 3)
4

,我不能想到一个很好的内置/单线程让我这样做(和我不是特别想复制这个函数,如果我不必)。任何想法?



(重要的是,生成的方法不会处理整个列表,这可能会很大。)

解决方案

在Python 2.6或更好的版本中:




  • next如果你想要 StopIteration


  • next((如果x> 3,则为x在the_iterable中为x)如果您要 default_value (例如 None),请输入$ default_value)



    )。

    注意,在这种情况下,你需要一个额外的一对圆括号括在生成器表达式中 - 当生成器表达式不是唯一的参数。




我看到大多数答案都坚决忽略 下一页 所以我假设由于一些神秘的原因,他们' re 100%专注于版本2.5和更旧 - 没有提到Python版本问题(但是我没有看到提及在答案,提及下一页内置,这就是为什么我认为有必要提供一个答案自己 - 至少正确的版本问题得到记录这种方式; - )。



在2.5中, .next() 迭代器方法立即引发 StopIteration 如果迭代器立即结束 - 即,对于你的用例,项在迭代中满足条件。如果您不在乎(即,您知道必须至少有一个令人满意的项目),那么只需使用 .next()一个genexp,一行下一个内置在Python 2.6和更好的)。



do 关心,包装的东西在一个函数,如你第一次表明你的Q似乎最好,而你提出的函数实现只是很好,你可以使用 itertools ,a for ...:break 循环,或genexp或 try / except StopIteration 功能的身体,作为各种答案建议。在这些替代品中没有太多的附加价值,所以我会去你首先提出的那个非常简单的版本。


In Python, I would like to get the first item from a list matching a condition. 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 (and I don't particularly want to copy this function around if I don't have to). Any ideas?

(It's important that the resulting method not process the entire list, which could be quite large.)

解决方案

In Python 2.6 or better:

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

    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)

    if you want default_value (e.g. None) to be returned instead.
    Note that you need an extra pair of parentheses around the generator expression in this case - they are needed always when the generator expression isn't the only argument.

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;-).

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).

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.

这篇关于从匹配条件的迭代器中获取第一个项目的最好方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆