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

喜你入骨 提交于 2019-11-30 01:45:42

You have to iterate through the filter object somehow. One way is to convert it to a list:

l = list(filter(lambda x: x > 3, n))

len(l)  # <--

But that might defeat the point of using filter() in the first place, since you could do this more easily with a list comprehension:

l = [x for x in n if x > 3]

Again, len(l) will return the length.

Al Hoo

This is an old question, but I think this question needs an answer using the map-reduce ideology. So here:

from functools import reduce

def ilen(iterable):
    return reduce(lambda sum, element: sum + 1, iterable, 0)

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

This is especially good if n doesn't fit in the computer memory.

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.

Generally, filter and reduce are not pythonic.

@arshajii metioned this solution:

len([x for x in n if x > 3])

This is quite simple, but is not describing what you exactly want to do, and it makes a list that may use some additional memory. A better solution is using sum with generator:

sum(1 for x in n if x > 3)

(See more about generator here: https://www.python.org/dev/peps/pep-0289/#rationale)

However, sum with generator is actually slower in most cases because of the implementation (tested in CPython 3.6.4):

In [1]: %timeit len([1 for x in range(10000000)])
356 ms ± 17.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [2]: %timeit sum(1 for x in range(10000000))
676 ms ± 7.05 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

Converting a filter to a list will take extra memory, which may not be acceptable for large amounts of data. You can find length of the filter object without converting it to a list:

sum(1 for _ in filter(lambda x: x > 3, n))

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