【发布时间】:2020-08-06 00:17:17
【问题描述】:
我正在尝试在包含列表的单个字典列表之间合并一些数据。如果它们匹配,则合并将基于“对象”键进行。如果匹配相同的值,也会添加到他们给定的“部分”。给定以下数据:
data = [
{
"semver":"1.0.0",
"sections":[
{
"name":"Add",
"messages":[
"add: comment here"
]
}
],
"object":"files.sh"
},
{
"semver":"1.0.0",
"sections":[
{
"name":"Add",
"messages":[
"add: Second comment here"
]
}
],
"object":"files.sh"
},
{
"semver":"1.0.0",
"sections":[
{
"name":"Fix",
"messages":[
"Comment here"
]
}
],
"object":"files.sh"
}
]
我希望最终实现这一目标
data = [
{
"semver":"1.0.0",
"sections":[
{
"name":"Add",
"messages":[
"add: comment here",
"add: Second comment here"
]
},
{
"name":"Fix",
"messages":[
"Fix: comment here"
]
}
],
"object":"files.sh"
},
]
for item in data:
for k, v in item.items():
print(k)
print(v)
任何指针或帮助将不胜感激。到目前为止,我正在遍历字典中的每个 k,v 对,但无法将我的头脑围绕在循环中两者之间的匹配上。
【问题讨论】:
-
您的编码尝试在哪里?请重复how to ask。
-
嘿@Prune,我只有一个数据循环,只是无法在循环中的两者之间进行匹配。刚刚用我目前拥有的内容更新了我的原始帖子。
标签: python list dictionary merge append