mohamed naseh
mohamed naseh

Reputation: 3

i need to Count through list

I have multiple lists and a big list of lists, I want to check if the big list have some of my small lists and count but I don't want redundancy , example:

list1 = [ "A" , "B" , "C" , "D"]
List2 = ['K' , "l" , "M"]

main_lists = [["A","C","K","L"] , ["B" , "C" , "M"]]

count1 = 0
count2 = 0

what I need is to cont like if I found any item in main list in my two lists I increase a counter but not redundant so my output of this case should be count 1 = 2 count 2 = 2

no important that A and C in the list of my list, just 1 of them will increase the count, what should I do

Upvotes: 0

Views: 41

Answers (1)

Dani Mesejo
Dani Mesejo

Reputation: 61930

IIUC, you could do the following:

list1 = ["A", "B", "C", "D"]
list2 = ['K', "l", "M"]

main_lists = [["A", "C", "K", "L"], ["B", "C", "M"]]
set1 = set(list1)
set2 = set(list2)

count1 = sum(any(ie in set1 for ie in e) for e in main_lists)
count2 = sum(any(ie in set2 for ie in e) for e in main_lists)

print(count1)
print(count2)

Upvotes: 1

Related Questions