Python filter / max combo - checking for empty iterator

后端 未结 4 1136
有刺的猬
有刺的猬 2021-01-12 08:44

(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 t

4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-12 09:09

    If you just want to check if the return of filter is empty, you might do (Python3)

    len(list(filter(lambda e : e == 2, [1,2,3])))

    But notice, hence filter is a generator if you this test twice, second time, you will receive a diferent result:

    len(list(filter(lambda e : e == 2, [1,2,3]))) len(list(filter(lambda e : e == 2, [1,2,3])))

    >>> 1

    >>> 1

    But:

    f = filter(lambda e : e == 2, [1,2,3]) len(list(f)) len(list(f))

    >>> 1

    >>> 0

提交回复
热议问题