How to better count occurrences of a list in another list and then list the results for each element using count

Viewed 169

I want a faster way for Pandas python to calculate number of occurrences of each element of listA in listB and make the number of counts into listC. for example

listA = [1,2,3,4,5,6,7]

listB = [1,1,5,4,8,3,4,7,9,2,1,4,7,6,2,4,5,6,3]

output:

listC = [3,2,2,4,2,2,2]
The number 1 appears 3 times, and 2 appears three times so on. This is just an example in my real work listA has 800 000 elements and listB has 2million elements. I am using the following code but it takes too long to process using Pandas.

listC =[listB.count(x) for x in listA]

2 Answers

You can use collections.Counter to speed up the task:

from collections import Counter

listA = [1, 2, 3, 4, 5, 6, 7]
listB = [1, 1, 5, 4, 8, 3, 4, 7, 9, 2, 1, 4, 7, 6, 2, 4, 5, 6, 3]

c = Counter(listB)

listC = [c.get(v) for v in listA]
print(listC)

Prints:

[3, 2, 2, 4, 2, 2, 2]

Or: Use np.bincount:

count_arr = np.bincount(listB)
print([count_arr[x] for x in listA])

Prints:

[3, 2, 2, 4, 2, 2, 2]

I suppose the idea to sort that huge data is out of the question?

Just that I think count() might work faster maybe if the data is sorted, because similar values are placed in adjacent position.

Related