Counting positive elements in a list with Python list comprehensions
Posted
by AntiRush
on Stack Overflow
See other posts from Stack Overflow
or by AntiRush
Published on 2010-05-24T20:26:22Z
Indexed on
2010/05/26
7:31 UTC
Read the original article
Hit count: 428
I have a list of integers and I need to count how many of them are > 0.
I'm currently doing it with a list comprehension that looks like this:
sum([1 for x in frequencies if x > 0])
It seems like a decent comprehension but I don't really like the "1"; it seems like a bit of a magic number. Is there a more Pythonish way to do this?
© Stack Overflow or respective owner