【发布时间】: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