# [Solved] Two list count specific elements and aggregate on spefic rule [closed]

There are many possible solutions. In my opininion this is the easiest:

``````import heapq

list1 = [2,5,7]
list2=[4,6,9]

counter1=0
counter2=0

sum1=0
sum2=0

full_list = list1 + list2
three_largest = heapq.nlargest(3,full_list)

for n in three_largest:
if n in list1:
list1.remove(n)
counter1+=1
sum1+=n
else:
list2.remove(n)
counter2+=1
sum2+=n

print(counter1)
print(counter2)
print(sum1)
print(sum2)
``````

Note that you made a mistake in your example for the values you provide. 9 is in list2

EDIT

Here is an example that deals with N highest numbers in M lists:

``````import heapq
from typing import List

list_of_lists = [[1,2,3,4],[4,3,7,12],[8,8,10,1]]

def quick_metric(n_highest,lists:List[List[int]]):
counters = [0 for i in range(len(lists))]
sums = [0 for i in range(len(lists))]
flat_list = [item for sublist in lists for item in sublist]
highest = heapq.nlargest(n_highest, flat_list)

for n in highest:
for x_list in lists:
if n in x_list:
x_list.remove(n)
counters[lists.index(x_list)]+=1
sums[lists.index(x_list)]+=n
break

print(counters)
print(sums)
print(highest)

quick_metric(3,list_of_lists)
``````

Output:

``````[0, 1, 2]
[0, 12, 18]
[12, 10, 8]
``````

solved Two list count specific elements and aggregate on spefic rule [closed]