【发布时间】:2021-07-31 13:12:32
【问题描述】:
我有一个这样的字典列表:
res = [{"isBlack": "yes"} , {"isWhite": "no"} , {"isRed": "yes"} , {"isWhite": "yes"} , {"isRed": "no"} , {"isBlack": "yes"}]
我需要按键值对对 res 进行分组,并使用 Counter 获取它们的计数,如下所示:
Counter({"isBlack:yes": 2 , "isWhite:no": 1, "isWhite:yes": 1 , "isRed:no": 1 , "isRed:yes": 1})
我试过这个:
count = Counter(res) ,但出现如下错误:
TypeError: unhashable type: 'dict'
还有什么我可以尝试的吗?
【问题讨论】:
标签: python python-3.x list dictionary counter