I am trying to find common elements (and the total number of occurrences) between two lists. For example, the intersection of these two lists:
a = [1, 1, 2, 3, 4, 5, 6, 7, 8, 1]
b = [1, 1, 3, 5, 7, 9]
should return Counter({1: 2, 3: 1, 5: 1, 7: 1})or something like that, for example. {1: 2, 3: 1, 5: 1, 7: 1}or [1, 1, 3, 5, 7](the order of the list does not matter).
I already have an approach that works:
cnts_a = Counter(a)
cnts_b = Counter(b)
cnts_a_b = Counter()
for key in set(cnts_a).intersection(cnts_b):
cnts_a_b[key] = min(cnts_a[key], cnts_b[key])
But maybe there is an easier (or faster) way?