So i have an array, say something like [5,2,2,0], is there a function to return the number of elements that pass a criterion?
Currently i’m doing this:
a = [5,2,2,0]
len([i for i in a if i > 0])
someone suggested this approach too:
sum(b > 0 for b in a)
but IMO this is really the same thing, just a little less readable.
Is there some method like this i could use:
def crit(x): return x > 0
a.count(criterion=crit)
You can use
filterfunctionlen(filter(crit, a))