Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting positive integer elements in a list with Python list comprehensions

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?

like image 877
fairfieldt Avatar asked May 24 '10 20:05

fairfieldt


People also ask

How do you count the number of positive numbers in a list Python?

Given a list of numbers, write a Python program to count positive and negative numbers in a List. Iterate each element in the list using for loop and check if num >= 0, the condition to check positive numbers. If the condition satisfies, then increase pos_count else increase neg_count.

How do you count integers in a list in Python?

The count() is a built-in function in Python. It will return you the count of a given element in a list or a string. In the case of a list, the element to be counted needs to be given to the count() function, and it will return the count of the element. The count() method returns an integer value.

How do you count occurrs of each element in a list Python?

The easiest way to count the number of occurrences in a Python list of a given item is to use the Python . count() method. The method is applied to a given list and takes a single argument. The argument passed into the method is counted and the number of occurrences of that item in the list is returned.


1 Answers

If you want to reduce the amount of memory, you can avoid generating a temporary list by using a generator:

sum(x > 0 for x in frequencies) 

This works because bool is a subclass of int:

>>> isinstance(True,int) True 

and True's value is 1:

>>> True==1 True 

However, as Joe Golton points out in the comments, this solution is not very fast. If you have enough memory to use a intermediate temporary list, then sth's solution may be faster. Here are some timings comparing various solutions:

>>> frequencies = [random.randint(0,2) for i in range(10**5)]  >>> %timeit len([x for x in frequencies if x > 0])   # sth 100 loops, best of 3: 3.93 ms per loop  >>> %timeit sum([1 for x in frequencies if x > 0]) 100 loops, best of 3: 4.45 ms per loop  >>> %timeit sum(1 for x in frequencies if x > 0) 100 loops, best of 3: 6.17 ms per loop  >>> %timeit sum(x > 0 for x in frequencies) 100 loops, best of 3: 8.57 ms per loop 

Beware that timeit results may vary depending on version of Python, OS, or hardware.

Of course, if you are doing math on a large list of numbers, you should probably be using NumPy:

>>> frequencies = np.random.randint(3, size=10**5) >>> %timeit (frequencies > 0).sum() 1000 loops, best of 3: 669 us per loop 

The NumPy array requires less memory than the equivalent Python list, and the calculation can be performed much faster than any pure Python solution.

like image 194
unutbu Avatar answered Sep 24 '22 02:09

unutbu