python: get number of items from list(sequence) with certain condition

前端 未结 5 789
没有蜡笔的小新
没有蜡笔的小新 2020-11-27 18:55

Assuming that I have a list with huge number of items.

l = [ 1, 4, 6, 30, 2, ... ]

I want to get the number of items from that list, where

5条回答
  •  孤独总比滥情好
    2020-11-27 19:25

    you could do something like:

    l = [1,2,3,4,5,..]
    count = sum(1 for i in l if my_condition(i))
    

    which just adds 1 for each element that satisfies the condition.

提交回复
热议问题