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

前端 未结 5 791
没有蜡笔的小新
没有蜡笔的小新 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:28

    You want a generator comprehension rather than a list here.

    For example,

    l = [1, 4, 6, 7, 30, 2]
    
    def my_condition(x):
        return x > 5 and x < 20
    
    print sum(1 for x in l if my_condition(x))
    # -> 2
    print sum(1 for x in range(1000000) if my_condition(x))
    # -> 14
    

    Or use itertools.imap (though I think the explicit list and generator expressions look somewhat more Pythonic).

    Note that, though it's not obvious from the sum example, you can compose generator comprehensions nicely. For example,

    inputs = xrange(1000000)      # In Python 3 and above, use range instead of xrange
    odds = (x for x in inputs if x % 2)  # Pick odd numbers
    sq_inc = (x**2 + 1 for x in odds)    # Square and add one
    print sum(x/2 for x in sq_inc)       # Actually evaluate each one
    # -> 83333333333500000
    

    The cool thing about this technique is that you can specify conceptually separate steps in code without forcing evaluation and storage in memory until the final result is evaluated.

提交回复
热议问题