2013-08-30 44 views
26

給出一個列表,是否有辦法獲得第一個非None值?而且,如果是這樣,那麼這樣做的pythonic方式是什麼?從列表中獲取第一個非None值

例如,我有:

  • a = objA.addreses.country.code
  • b = objB.country.code
  • c = None
  • d = 'CA'

在這種情況下,如果是沒有的話,我想獲得灣如果a和b都是None,我想得到d。

目前我正在做一些沿着(((a or b) or c) or d)的行,有沒有另一種方式?

回答

62

您可以使用next()

>>> a = [None, None, None, 1, 2, 3, 4, 5] 
>>> next(item for item in a if item is not None) 
1 

如果列表中只包含諾內斯,它會拋出異常StopIteration。如果你想在這種情況下的默認值,這樣做:

>>> a = [None, None, None] 
>>> next((item for item in a if item is not None), 'All are Nones') 
All are Nones 
+3

我會添加一個'try:'',除了StopIteration:'來處理只包含無項目的列表。但這是一個很好的答案。 – Germano

+1

@Germano更新了答案,請檢查。順便說一下,喬恩的答案應該被接受 - 他首先提出了一個解決方案,無單列表。 – alecxe

+0

實際上我認爲Jon的回答是不正確的:它只返回'True'或'False',而不是列表中的第一個不是'None'項。 – Germano

4

從以下(你可以一個班輪,如果你想)適應:

values = (a, b, c, d) 
not_None = (el for el in values if el is not None) 
value = next(not_None, None) 

這需要第一個非None值,或者返回None

+0

我不明白。不,這是印刷只是「真」或「假」? – thefourtheye

+0

@thefourtheye是 - 有一個腦打嗝...感謝指出它 –

2

first_true是在Python 3 docs發現了一個itertools配方:

def first_true(iterable, default=False, pred=None): 
    """Returns the first true value in the iterable. 

    If no true value is found, returns *default* 

    If *pred* is not None, returns the first item 
    for which pred(item) is true. 

    """ 
    # first_true([a,b,c], x) --> a or b or c or x 
    # first_true([a,b], x, f) --> a if f(a) else b if f(b) else x 
    return next(filter(pred, iterable), default) 

人們可以選擇實現後者的配方或導入more_itertools,圖書館附帶itertools食譜和更多:

> pip install more_itertools 

用途:

import more_itertools as mit 

a = [None, None, None, 1, 2, 3, 4, 5] 
mit.first_true(a, pred=lambda x: x is not None) 
# 1 

a = [None, None, None] 
mit.first_true(a, default="All are None", pred=lambda x: x is not None) 
# 'All are None' 

爲什麼使用謂詞?

「第一個非None」項目與「第一個True」項目不同,例如, [None, None, 0]其中0是第一個非None,但它不是第一個True項目。謂詞允許first_true可用,以確保在可迭代中第一次看到的非None,falsey項仍然返回(例如0,False)而不是默認值。

a = [None, None, None, False] 
mit.first_true(a, default="All are None", pred=lambda x: x is not None) 
# 'False' 
相關問題