比较两个字典并在键匹配的位置追加嵌套对象的 Pythonic 方法
Pythonic way toCompare two Dictionaries and Append Nested Object where keys match
Python 有工作代码的新手,但希望了解是否有更好的方法。
问题陈述 - 一个 python 字典包含有关卷的详细信息,第二个字典包含有关卷快照的详细信息。生成一个 json 文档,每个卷作为键,如果该卷有快照,则将快照字典对象作为嵌套字典添加到相应卷的快照键下。如果该卷没有任何快照,只需在相应卷的快照键下附加一个空对象作为嵌套字典。
- 是否有更pythonic的方法来解决这个挑战(也许是理解)?
- 我更新原版词典是不是搞错了?
import json
volume_dict = {
"vol-04f": {
"Name": "db_server",
"State": "in-use",
"DataType": "Public",
"Attachments": [
"i-0fc"
]
},
"vol-0cc": {
"Name": "app_server",
"State": "in-use",
"DataType": "Public",
"Attachments": [
"i-051"
]
}
}
# Snapshot Dictionary
snapshot_dict = {
"vol-04f": [
{
"snap-0086": {
"Date": 20210911,
"SnapshotState": "completed"
}
},
{
"snap-06ff": {
"Date": 20210910,
"SnapshotState": "completed"
}
},
{
"snap-0263": {
"Date": 20210919,
"SnapshotState": "completed"
}
}
]
}
for volume_key, volume_value in volume_dict.items():
if volume_key in snapshot_dict:
value = {}
snapshot_value = snapshot_dict.get(volume_key)
value["Snapshots"] = snapshot_value
else:
value = {}
value["Snapshots"] = {}
volume_dict[volume_key].update(value)
print(json.dumps(volume_dict))
期望的输出
{
"vol-04f": {
"DataType": "Public",
"State": "in-use",
"Name": "db_server",
"Snapshots": [
{
"snap-0086": {
"Date": 20210911,
"SnapshotState": "completed"
}
},
{
"snap-06ff": {
"Date": 20210910,
"SnapshotState": "completed"
}
},
{
"snap-0263": {
"Date": 20210919,
"SnapshotState": "completed"
}
}
],
"Attachments": [
"i-0fc"
]
},
"vol-0cc": {
"DataType": "Public",
"State": "in-use",
"Name": "app_server",
"Snapshots": {},
"Attachments": [
"i-051"
]
}
}
dict.get
有默认值,当找不到键时返回,你可以利用它:
for k, v in volume_dict.items():
v["Snapshots"] = snapshot_dict.get(k, {})
print(volume_dict)
打印:
{
"vol-04f": {
"Name": "db_server",
"State": "in-use",
"DataType": "Public",
"Attachments": ["i-0fc"],
"Snapshots": [
{"snap-0086": {"Date": 20210911, "SnapshotState": "completed"}},
{"snap-06ff": {"Date": 20210910, "SnapshotState": "completed"}},
{"snap-0263": {"Date": 20210919, "SnapshotState": "completed"}},
],
},
"vol-0cc": {
"Name": "app_server",
"State": "in-use",
"DataType": "Public",
"Attachments": ["i-051"],
"Snapshots": {},
},
}
Q1
如果你想使用列表推导式,这就是方法
[volume_dict[volume_key].update({"Snapshots": snapshot_dict[volume_key] if volume_key in snapshot_dict else {}}) for volume_key, volume_value in volume_dict.items()]
更简单的方法是
for volume_key, volume_value in volume_dict.items():
volume_dict[volume_key]["Snapshots"] = snapshot_dict[volume_key] if volume_key in snapshot_dict else {}
Q2
那将根据您的要求。如果以后想用原来的volume_dict,最好弄个副本更新一下。
Python 有工作代码的新手,但希望了解是否有更好的方法。
问题陈述 - 一个 python 字典包含有关卷的详细信息,第二个字典包含有关卷快照的详细信息。生成一个 json 文档,每个卷作为键,如果该卷有快照,则将快照字典对象作为嵌套字典添加到相应卷的快照键下。如果该卷没有任何快照,只需在相应卷的快照键下附加一个空对象作为嵌套字典。
- 是否有更pythonic的方法来解决这个挑战(也许是理解)?
- 我更新原版词典是不是搞错了?
import json
volume_dict = {
"vol-04f": {
"Name": "db_server",
"State": "in-use",
"DataType": "Public",
"Attachments": [
"i-0fc"
]
},
"vol-0cc": {
"Name": "app_server",
"State": "in-use",
"DataType": "Public",
"Attachments": [
"i-051"
]
}
}
# Snapshot Dictionary
snapshot_dict = {
"vol-04f": [
{
"snap-0086": {
"Date": 20210911,
"SnapshotState": "completed"
}
},
{
"snap-06ff": {
"Date": 20210910,
"SnapshotState": "completed"
}
},
{
"snap-0263": {
"Date": 20210919,
"SnapshotState": "completed"
}
}
]
}
for volume_key, volume_value in volume_dict.items():
if volume_key in snapshot_dict:
value = {}
snapshot_value = snapshot_dict.get(volume_key)
value["Snapshots"] = snapshot_value
else:
value = {}
value["Snapshots"] = {}
volume_dict[volume_key].update(value)
print(json.dumps(volume_dict))
期望的输出
{
"vol-04f": {
"DataType": "Public",
"State": "in-use",
"Name": "db_server",
"Snapshots": [
{
"snap-0086": {
"Date": 20210911,
"SnapshotState": "completed"
}
},
{
"snap-06ff": {
"Date": 20210910,
"SnapshotState": "completed"
}
},
{
"snap-0263": {
"Date": 20210919,
"SnapshotState": "completed"
}
}
],
"Attachments": [
"i-0fc"
]
},
"vol-0cc": {
"DataType": "Public",
"State": "in-use",
"Name": "app_server",
"Snapshots": {},
"Attachments": [
"i-051"
]
}
}
dict.get
有默认值,当找不到键时返回,你可以利用它:
for k, v in volume_dict.items():
v["Snapshots"] = snapshot_dict.get(k, {})
print(volume_dict)
打印:
{
"vol-04f": {
"Name": "db_server",
"State": "in-use",
"DataType": "Public",
"Attachments": ["i-0fc"],
"Snapshots": [
{"snap-0086": {"Date": 20210911, "SnapshotState": "completed"}},
{"snap-06ff": {"Date": 20210910, "SnapshotState": "completed"}},
{"snap-0263": {"Date": 20210919, "SnapshotState": "completed"}},
],
},
"vol-0cc": {
"Name": "app_server",
"State": "in-use",
"DataType": "Public",
"Attachments": ["i-051"],
"Snapshots": {},
},
}
Q1
如果你想使用列表推导式,这就是方法
[volume_dict[volume_key].update({"Snapshots": snapshot_dict[volume_key] if volume_key in snapshot_dict else {}}) for volume_key, volume_value in volume_dict.items()]
更简单的方法是
for volume_key, volume_value in volume_dict.items():
volume_dict[volume_key]["Snapshots"] = snapshot_dict[volume_key] if volume_key in snapshot_dict else {}
Q2
那将根据您的要求。如果以后想用原来的volume_dict,最好弄个副本更新一下。