Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

count the number of occurrences of a certain value in a dictionary in python?

Tags:

If I have got something like this:

D = {'a': 97, 'c': 0 , 'b':0,'e': 94, 'r': 97 , 'g':0} 

If I want for example to count the number of occurrences for the "0" as a value without having to iterate the whole list, is that even possible and how?

like image 458
RowanX Avatar asked Jan 21 '18 21:01

RowanX


People also ask

How do you count how many times something appears in a dictionary in Python?

If you want to count the occurrences of each value in a Python dictionary, you can use the collections. Counter() function on the dictionary values. It returns the number of times each value occurs in the dictionary.

How do you count the number of specific elements in a list in Python?

We can use the len( ) function to return the number of elements present in the list.

How do you determine the number of elements that are stored in a dictionary?

To find the number of elements stored in a dictionary we can use the len() function. To find the size of a dictionary in bytes we can use the getsizeof() function of the sys module. To count the elements of a nested dictionary, we can use a recursive function.

How do you count how many times a value appears in a list?

Count how often a single value occurs by using the COUNTIF function. Use the COUNTIF function to count how many times a particular value appears in a range of cells.


1 Answers

As mentioned in THIS ANSWER using operator.countOf() is the way to go but you can also use a generator within sum() function as following:

sum(value == 0 for value in D.values()) # Or the following which is more optimized  sum(1 for v in D.values() if v == 0) 

Or as a slightly more optimized and functional approach you can use map function by passing the __eq__ method of the integer as the constructor function.

sum(map((0).__eq__, D.values())) 

Benchmark:

In [15]: D = dict(zip(range(1000), range(1000)))  In [16]: %timeit sum(map((0).__eq__, D.values())) 49.6 µs ± 770 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)  In [17]: %timeit sum(v==0 for v in D.values()) 60.9 µs ± 669 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)  In [18]: %timeit sum(1 for v in D.values() if v == 0) 30.2 µs ± 515 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)  In [19]: %timeit countOf(D.values(), 0) 16.8 µs ± 74.1 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each) 

Note that although using map function in this case may be more optimized, but in order to have a more comprehensive and general idea about the two approaches you should run the benchmark for relatively large datasets as well. Then, you can use the most proper approach based on the structure and amount of data you have.

like image 85
Mazdak Avatar answered Sep 22 '22 05:09

Mazdak