How to find the length of a “filter” object in python

前端 未结 5 1512
面向向阳花
面向向阳花 2020-12-08 09:18
>>> n = [1,2,3,4]

>>> filter(lambda x:x>3,n)


>>> len(filter(lambda x:x>3,n))
Traceback         


        
5条回答
  •  情话喂你
    2020-12-08 09:56

    The docs for python 3 say it returns an iterator

    "Construct an iterator from those elements of iterable for which function returns true."

    In python 2 it returned a list: see here. You will need to iterate the filter object to find its length.

提交回复
热议问题