gpt4 book ai didi

python - 列表中的字典::合并相同键的值

转载 作者:行者123 更新时间:2023-11-28 22:01:36 26 4
gpt4 key购买 nike

<分区>

我在 Python 列表中有多个字典,如下所示

[{"color": "#CC3", "values": {"y": 83, "x": 9}, "key": 105},
{"color": "#CC3", "values": {"y": 123, "x": 10}, "key": 105},
{"color": "#FF9", "values": {"y": 96, "x": 11}, "key": 106},
{"color": "#33F", "values": {"y": 80, "x": 12}, "key": 104},
{"color": "#CC3", "values": {"y": 117, "x": 13}, "key": 105},
{"color": "#CC3", "values": {"y": 115, "x": 14}, "key": 105},
{"color": "#CC3", "values": {"y": 102, "x": 15}, "key": 105},
{"color": "#FF9", "values": {"y": 111, "x": 16}, "key": 106},
{"color": "#FF9", "values": {"y": 33, "x": 17}, "key": 106}]

在上面包含字典的列表中,有称为“键”的键,其值相同,例如105、106等,在那些字典中,“值”的值各不相同,例如"values": {"y": 83, "x": 9}"values": {"y": 117, "x": 13} 105.

我想为同一个“key=xxx”字典添加并带入一个字典中的值

例如,对于 key=105 的字典,我想聚合“值”:[{"y": 83, "x": 9},{"y": 117, "x ": 13}] 在这样的列表中说

最终的合并输出应该保留上面引用的原始结构

[{"color"="...","values"=[{...},{...}],"key"="..."},....]

完成此任务的最佳方法是什么?

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com