Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Counting the occurrence from 2 big arrays

Tags:

python

I have the following script that counts the occurrence of values from one array to another

array_1 = [1,2,0,5,7,0]
array_2 = [1,0,1,1,9,6]
# on array 2 there are 3 occurrence of 1, and 1 occurrence of zero, but because there is another zero at array_1 add 1 more. 3+2 = 5

for r in array_1:
     total_count = total_count + array_2.count(r)

print("total sum: {0}".format(total_count))

its ok when dealing with small array size, but struggles when the array size increases (1 million for array_1 and 1 million array_2). Is there a better to approach this?

sorry for the confusion, i updated the question a little bit.

like image 999
Led Avatar asked May 19 '26 06:05

Led


2 Answers

Note: The answer by @Netwave is five time faster.

You can use collections.Counter. It is be faster, because it only iterates ones of the list.

from collections import Counter

array_1 = [1,2,0,5,7]
array_2 = [1,0,1,1,9]

c = Counter(array_1)
total_count = sum(c[x] for x in array_2)

print("total sum: {0}".format(total_count))
like image 116
MegaIng Avatar answered May 22 '26 01:05

MegaIng


Use a set instead of a list:

array1_set = set(array_1)
total_count = sum(1 for x in array_2 if x in array1_set)
like image 27
Netwave Avatar answered May 22 '26 00:05

Netwave



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!