如何使用itertools.groupby格式化字典列表?

时间:2016-04-11 20:03:01

标签: python json dictionary python-3.5

尝试找到将dicts列表格式化为特定json格式的最佳方法。目前我正在手动循环遍历数组。

我的阵列:

a = [{'sales': 2100, 'department_id': 66, 'department_name': 'dog', 'month':11},
     {'sales': 3900, 'department_id': 22, 'department_name': 'cat', 'month':12},
     {'sales': 2100, 'department_id': 66, 'department_name': 'cat','month':11},
     {'sales': 3900, 'department_id': 22, 'department_name': 'dog','month':12}]

预期结果:

b = [{"result":[{"month":11, 'sales': 2100},
                {"month":12, 'sales': 3900},]
      "meta":{"name":"cat"}
      "department_id": 22
     },
     {"result":[{"month":11, 'sales': 2100},
                {"month":12, 'sales': 3900},]
      "meta":{"name":"dog"}
      "department_id": 66
     }]

虽然可以按groupby排序,但我不想再次使用它来获取名称元数据。还有另一个有效的dict group bys库吗?

1 个答案:

答案 0 :(得分:1)

如果您只想按 department_id 分组,可以使用另一个dict进行分组,使用defaultdict按ID分组并根据需要构建新的k / v对,不需要对数据进行排序:

a = [{'sales': 2100, 'department_id': 66, 'department_name': 'dog', 'month': 11},
     {'sales': 3900, 'department_id': 22, 'department_name': 'cat', 'month': 12},
     {'sales': 2100, 'department_id': 66, 'department_name': 'cat', 'month': 11},
     {'sales': 3900, 'department_id': 22, 'department_name': 'dog', 'month': 12}]

from collections import defaultdict

dct = defaultdict(lambda: {"result": [], "meta": "", "department_id": ""})

for d in a:
    _id = d['department_id']
    dct[_id]["result"].append({"sales": d["sales"], "month": d["month"]})
    dct[_id]["meta"] = d['department_name']
    dct[_id]["department_id"] = _id

from pprint import pprint as pp
pp(dct.values())

哪个会给你:

[{'department_id': 66,
  'meta': 'cat',
  'result': [{'month': 11, 'sales': 2100}, {'month': 11, 'sales': 2100}]},
 {'department_id': 22,
  'meta': 'dog',
  'result': [{'month': 12, 'sales': 3900}, {'month': 12, 'sales': 3900}]}]