![](/style/images/good.png)
![](/style/images/bad.png)
Is there a function to give the number of elements in a list that passes a crite...
source link: https://www.codesd.com/item/is-there-a-function-to-give-the-number-of-elements-in-a-list-that-passes-a-criterion.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Is there a function to give the number of elements in a list that passes a criterion?
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 filter
function len(filter(crit, a))
Related Articles
Is there an algorithm for determining the number of elements in contiguous regions?
Watir webdriver print the number of elements from a list
How to find the number of elements in an arraylist that falls in a certain range?
Is there a practical limit to the number of elements in a set sorted into redis?
Java - How to find the number of elements in a list in another list
Python, datasframe: Duplication of lines according to the number of elements in a list and classification of the resulting lines
Find the number of elements less than or equal to k in a multiset
Effective way to count the number of elements smaller (larger) than the threshold in a sorted list
Effective algorithm to find the number of elements less than a query
Count the number of items in my list of tables
How to get the number of elements in an array, the Skill () and Length of the Extension Method gives the size of the matrix.
Is there a better way to calculate the number of elements in a one-dimensional array?
Is there a way to limit the number of columns in Hbase
Does array.length give you the number of elements in the table or the size of the array?
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK