【发布时间】:2021-11-10 04:25:42
【问题描述】:
我有问题。
Input=[
{'id':uuid1(),'name':'mem','point':[{'second':0,'severity':'infor'}],'date':'2021-09-15','PO':5},
{'id':uuid1(),'name':'cpu','point':[{'second':10,'severity':'warning'}],'date':'2021-09-13','PO':3},
{'id':uuid1(),'name':'cpu','point':[{'second':20,'severity':'critical'}],'date':'2021-09-14','PO':3},
{'id':uuid1(),'name':'cpu','point':[{'second':30,'severity':'infor'}],'date':'2021-09-15','PO':3}]
我将在这个数组中进行比较。如果同名,我会合并这个dict的所有值。
这意味着结合关键“点”的所有价值。我将拥有多字典的关键列表“点”
我们有很多重复的键名 = 'cpu'。因此,如果值相同,我将合并所有值。你看到我们有相同的 PO 值(PO=3,名称=cpu)。如果不同的值,我们将取最后一个值(最后一个值 'date':'2021-09 -15').最后,我们将关键点列表中的dicts一起追加
想要的结果
Output=[
{'id':uuid1(),'name':'mem','point':[{'second':0,'severity':'infor'}],'date':'2021-09-15','PO':5},
{'id':uuid1(),'name':'cpu','point':[{'second':10,'severity':'warning'},
{'second':20,'severity':'critical'},
{'second':30,'severity':'infor'}],'date':'2021-09-15','PO':3}
]
注意:我使用 python 3.6
我希望有人能帮助我。非常感谢你
【问题讨论】:
-
你想把字典合并到什么地方??你能正确解释
We have many duplicate key name = 'cpu'. So I will combine all value if the same value.U see we have PO the same value (PO=3 with name=cpu).If different value, we will take the last value(last value 'date':'2021-09-15').Finally, we will append the dicts in the key point list togethe吗?它不是很清楚。可以举个例子吗? -
嗨阿克沙伊。我会解释:如果重复的键名。我们将结合价值。 dict的每个点中的关键点+ =值。并且 date' 将使用此键名获取最后一个值 (get 'date':'2021-09-15')
标签: python list dictionary merge