第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從匹配條件的迭代器中獲取第一項。

從匹配條件的迭代器中獲取第一項。

猛跑小豬 2019-06-28 10:54:11
從匹配條件的迭代器中獲取第一項。我想從符合條件的列表中獲得第一項。重要的是,生成的方法不能處理整個列表,這可能相當大。例如,以下功能就足夠了:def first(the_iterable, condition = lambda x: True):     for i in the_iterable:         if condition(i):             return i這個函數可以使用如下所示:>>> first(range(10))0>>> first(range(10), lambda i: i > 3)4然而,我想不出一個好的內置/一個班輪讓我這樣做。如果沒有必要的話,我不特別想復制這個函數。是否有一個內置的方式,以獲得第一個項目匹配的條件?
查看完整描述

3 回答

?
POPMUISE

TA貢獻1765條經驗 獲得超5個贊

作為一個可重用、文檔化和測試的功能

def first(iterable, condition = lambda x: True):
    """
    Returns the first item in the `iterable` that
    satisfies the `condition`.

    If the condition is not given, returns the first item of
    the iterable.

    Raises `StopIteration` if no item satysfing the condition is found.

    >>> first( (1,2,3), condition=lambda x: x % 2 == 0)
    2
    >>> first(range(3, 100))
    3
    >>> first( () )
    Traceback (most recent call last):
    ...
    StopIteration
    """

    return next(x for x in iterable if condition(x))


查看完整回答
反對 回復 2019-06-28
  • 3 回答
  • 0 關注
  • 683 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號