2015-09-20 104 views
0

有沒有更好的方法來完成以下模式?獲取最小值,非空

cost = min(t_data.sd_retail_price or 100000, t_data.hd_retail_price or 100000) 

基本上,我正在尋找最低的非空值。

+3

是這樣的,你在找什麼? http://stackoverflow.com/questions/21084714/find-the-lowest-value-that-is-not-null-using-python – Hayden

回答

1

你可以做這樣的事情:

cost = min(x for x in (t_data.sd_retail_price, t_data.hd_retail_price, 100000) if x) 

如果t_data.sd_retail_pricet_data.hd_retail_price都沒有,那麼你會留下100000作爲一個結果。

2

試試這個:

cost = min(filter(None, (t_data.sd_retail_price, t_data.hd_retail_price, 100000)))