我在这里扩展我的原始问题:How to append/merge list of dictionaries?。
我正在尝试在其中包含列表的单个词典列表之间合并一些数据。如果“ object”和“ semver”键匹配,则会进行合并。如果匹配相同的值,还将添加到其给定的“部分”中。给定以下数据:
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"
},
{
"semver":"2.0.0",
"sections":[
{
"name":"Fix",
"messages":[
"2.0.0 Fix Comment here"
]
}
],
"object":"files.sh"
},
{
"semver":"2.0.0",
"sections":[
{
"name":"Add",
"messages":[
"2.0.0 Add Comment here"
]
}
],
"object":"files.sh"
},
{
"semver":"2.0.0",
"sections":[
{
"name":"Add",
"messages":[
"2.0.0 comment for the NewFile"
]
}
],
"object":"NewFile.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"
},
{
"semver":"2.0.0",
"sections":[
{
"name":"Add",
"messages":[
"2.0.0 Add comment here",
]
},
{
"name":"Fix",
"messages":[
"2.0.0 Fix Comment here"
]
}
],
"object":"files.sh"
},
{
"semver":"2.0.0",
"sections":[
{
"name":"Add",
"messages":[
"2.0.0 comment for the NewFile"
]
}
],
"object":"NewFile.sh"
},
]
代码块
objects = {} # mapping for object: object_data with sections
sections = defaultdict(list) # mapping for object: all sections
for d in data:
print(d["semver"])
for k, v in list(d.items()):
if v == d["semver"]:
try:
section = d.pop("sections")
sections[d["object"]].extend(section)
objects[d["object"]] = d # populate with object data without sections
except Exception as e:
print(e)
pass
output = []
for object_name, object_data in objects.items():
object_data["sections"] = sections[object_name]
output.append(object_data)
到目前为止,我正在遍历dict
中的每个k,v对,但是无法绕过两个版本之间的匹配并在循环中追加到该特定的dict
。
我在这里扩展我的原始问题:如何追加/合并字典列表?。我正在尝试在其中包含列表的单个词典列表之间合并一些数据。合并会发生...
应进行一些更改: