简体   繁体   中英

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

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()))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM