【问题标题】:How do I group this list of dicts by the Key如何按键对这个字典列表进行分组
【发布时间】:2022-06-17 21:10:27
【问题描述】:

我是 python 新手。在 Python 中,我想通过 'zrepcode' 和 'channelid' 对下面的目录列表进行分组

代码sn-p如下:

list1 = [
    {'Item':'abc' ,'zrepcode': '1111', 'channelid': 2, 'total': 10},
    {'Item':'def' ,'zrepcode': '2222', 'channelid': 2, 'total': 12},
    {'Item':'abc' ,'zrepcode': '1111', 'channelid': 3, 'total': 5},
    {'Item':'def' ,'zrepcode': '2222', 'channelid': 4, 'total': 6}
]

按“zrepcode”键分组后的输出列表及其关联的“channelid”应该是这样的

list1 = [
    {'Item':'abc' ,'zrepcode': '1111', 'channels': [{channelid : 2, total =10}, {channelid : 3, total =5}]},
    {'Item':'def' ,'zrepcode': '2222', 'channels': [{channelid : 2, total =12}, {channelid : 4, total =6}]}
]

以上是预期的输出,现在不知道如何实现?

【问题讨论】:

  • 您有我们可以合作的尝试吗?

标签: python python-3.x list dictionary group-by


猜你喜欢
  • 2013-04-17
  • 1970-01-01
  • 2020-03-03
  • 2023-04-11
  • 1970-01-01
  • 1970-01-01
  • 2014-02-11
  • 2019-02-08
  • 1970-01-01
相关资源
最近更新 更多