Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching/Counting lists in python dictionary

I have a dictionary {x: [a,b,c,d], y: [a,c,g,f,h],...}. So the key is one variable with the value being a list (of different sizes).

My goal is to match up each list against every list in the dictionary and come back with a count of how many times a certain list has been repeated.

I tried this but does not seem to work:

count_dict = {}
counter = 1
for value in dict.values():
  count_dict[dict.key] = counter
  counter += 1
like image 648
Bilal Avatar asked Mar 15 '23 03:03

Bilal


1 Answers

You could map the lists to tuples so they can be used as keys and use a Counter dict to do the counting:

from collections import Counter 

count = Counter(map(tuple, d.values()))
like image 194
Padraic Cunningham Avatar answered Mar 24 '23 15:03

Padraic Cunningham