Count non zero elements in list python

numpy.count_nonzero[a, axis=None, *, keepdims=False][source]#

Counts the number of non-zero values in the array a.

The word “non-zero” is in reference to the Python 2.x built-in method __nonzero__[] [renamed __bool__[] in Python 3.x] of Python objects that tests an object’s “truthfulness”. For example, any number is considered truthful if it is nonzero, whereas any string is considered truthful if it is not the empty string. Thus, this function [recursively] counts how many elements in a [and in sub-arrays thereof] have their __nonzero__[] or __bool__[] method evaluated to True.

Parameters aarray_like

The array for which to count non-zeros.

axisint or tuple, optional

Axis or tuple of axes along which to count non-zeros. Default is None, meaning that non-zeros will be counted along a flattened version of a.

New in version 1.12.0.

keepdimsbool, optional

If this is set to True, the axes that are counted are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

New in version 1.19.0.

Returnscountint or array of int

Number of non-zero values in the array along a given axis. Otherwise, the total number of non-zero values in the array is returned.

See also

nonzero

Return the coordinates of all the non-zero values.

Examples

>>> np.count_nonzero[np.eye[4]]
4
>>> a = np.array[[[0, 1, 7, 0],
...               [3, 0, 2, 19]]]
>>> np.count_nonzero[a]
5
>>> np.count_nonzero[a, axis=0]
array[[1, 1, 2, 1]]
>>> np.count_nonzero[a, axis=1]
array[[2, 3]]
>>> np.count_nonzero[a, axis=1, keepdims=True]
array[[[2],
       [3]]]

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.

How do you count nonzero entries in NumPy array?

The Numpy count_nonzero[] function is used to give the count of the nonzero elements present in the multidimensional array. With the help of this function, we can find the count of the elements in the multidimensional array which are not zero. This function has 3 parameters as arr, axis and, keepdims.

Is there a NumPy count?

NumPy: count[] function count[] function returns an array with the number of non-overlapping occurrences of substring sub in the range [start, end]. Input an array_like of string or unicode.

How do you count values in an array in Python?

You can use the following methods to count the occurrences of elements in a NumPy array:.
Method 1: Count Occurrences of a Specific Value np. count_nonzero[x == 2].
Method 2: Count Occurrences of Values that Meet One Condition np. ... .
Method 3: Count Occurrences of Values that Meet One of Several Conditions np..

How do you count the number of zeros in an array in Python?

To count all the zeros in an array, simply use the np. count_nonzero[] function checking for zeros. It returns the count of elements inside the array satisfying the condition [in this case, if it's zero or not].

Chủ Đề