Python filter / max combo - checking for empty iterator

匿名 (未验证) 提交于 2019-12-03 08:54:24

问题:

(Using Python 3.1)

I know this question has been asked many times for the general question of testing if iterator is empty; obviously, there's no neat solution to that (I guess for a reason - an iterator doesn't really know if it's empty until it's asked to return its next value).

I have a specific example, however, and was hoping I can make clean and Pythonic code out of it:

#lst is an arbitrary iterable #f must return the smallest non-zero element, or return None if empty def f(lst):   flt = filter(lambda x : x is not None and x != 0, lst)   if # somehow check that flt is empty     return None   return min(flt)

Is there any better way to do that?

EDIT: sorry for the stupid notation. The parameter to the function is indeed an arbitrary iterable, rather than a list.

回答1:

def f(lst):   flt = filter(lambda x : x is not None and x != 0, lst)   try:     return min(flt)   except ValueError:     return None

min throws ValueError when the sequence is empty. This follows the common "Easier to Ask for Forgiveness" paradigm.

EDIT: A reduce-based solution without exceptions

from functools import reduce def f(lst):   flt = filter(lambda x : x is not None and x != 0, lst)   m = next(flt, None)   if m is None:     return None   return reduce(min, flt, m)


回答2:

def f(lst):     # if you want the exact same filtering as the original, you could use     # lst = [item for item in lst if (item is not None and item != 0)]      lst = [item for item in lst if item]     if lst: return min(lst)     else: return None

the list comprehension only allows items that don't evaluate to boolean false (which filters out 0 and None)

an empty list i.e. [] will evaluate to False, so "if lst:" will only trigger if the list has items



回答3:

you can go for reduce expression too return reduce(lambda a,b: a<b and a or b,x) or None



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!