如何获得字典列表中唯一值的计数?

时间:2019-03-20 22:16:59

标签: python

这是我的json(词典列表)的简短示例:

[
  {
    "product_type": "widget",
    "id": "1744556-ghh56h-4633",
    "manufacture_id": "AAB4567",
    "language": "en",
    "store_ids": [
      416835,
      456145
    ],
    "name": "Best Widget",
    "origin": "US",
    "manufactured": "2018-08-26",
    "uri": "https://bobswidgets.com/best_widget",
    "image_uris": {
      "small": "https://bobswidgets.com/small/best_widget_sm.jpg",
      "normal": "https://bobswidgets.com/medium/best_widget_md.jpg",
      "large": "https://bobswidgets.com/large/best_widget_lg.jpg"
    },
    "manufacture_cost": "12.50"
  },
  {
    "product_type": "widget",
    "id": "0956786-dje596-3904",
    "manufacture_id": "BCD13D",
    "language": "en",
    "store_ids": [
      "014329",
      "40123"
    ],
    "name": "Best Widget2",
    "origin": "US",
    "manufactured": "2018-10-03",
    "uri": "https://bobswidgets.com/best_widget_2",
    "image_uris": {
      "small": "https://bobswidgets.com/small/best_widget2_sm.jpg",
      "normal": "https://bobswidgets.com/medium/best_widget2_md.jpg",
      "large": "https://bobswidgets.com/large/best_widget2_lg.jpg"
    },
    "manufacture_cost": "13.33"
  }
]

我正在尝试获取一些key:value对的唯一值的计数。我已经试过了这段代码,并且它为每个键提供了很好的计数,但是这几乎没有用,因为计数几乎都是相同的。

from collections import Counter
from itertools import chain

counts = Counter(chain.from_iterable(e.keys() for e in card_dict))
for key, count in counts.most_common():
    print('{}: {}'.format(key, count))

我试图这样做来获取值而不是键,但得到一个错误。 TypeError: unhashable type: 'list'也几乎没有用,因为我在json文件中有ID,而且ID是唯一的。

counts = Counter(chain.from_iterable(e.values() for e in card_dict))
for value, count in counts.most_common():
    print('{}: {}'.format(value, count))

我想做的就是在几个键上输入零。例如语言key:value。我想算一下那些不是“ en”的数字。

输出类似于: “非英语的实体是:” 语言:12345

1 个答案:

答案 0 :(得分:0)

在python中,元组是可哈希的,因此您可以将元组用作字典中的键,而且dict.items()返回(key, value)元组。如果您将count更改为:

,请使用这两句话。
counts = Counter(chain.from_iterable(e.items() for e in card_dict))

如果某些值实际上是值列表或简单(非嵌套)字典,则可以在构建Counter之前将它们转换为元组:

def tuplify(card):
    if isinstance(card, list):
        return tuple(card)
    if isinstance(card, dict):
        return tuple(card.items())
    return card

card_dict = [{k: tuplify(v) for k, v in card.items()} for card in card_dict]