[英]Matching/Counting lists in python dictionary
I have a dictionary {x: [a,b,c,d], y: [a,c,g,f,h],...}
. 我有一本字典{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: 您可以将列表映射到元组,以便它们可以用作键并使用Counter
dict进行计数:
from collections import Counter
count = Counter(map(tuple, d.values()))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.