python - 如何将字典列表添加到另一个字典?
问题描述
我有这本带键的字典“属性”,livenessTests
而它又livenessTests
是一个字典列表。如何添加/附加livenessTests
到另一个字典json
?
properties = {
'livenessTests': [
{
'name':'http' + '_' + 'livenesstest',
'testObject':'/default.html'
},
{
'name':'https' + '_' + 'livenesstest',
'testObject':'/default.html'
}
]
生成的“json”目录应该类似于
json : {
"acg": {
"id": "1.87",
"name": "Internal"
},
"asmappings": [],
"cidrMaps": [],
"properties": {"livenessTests" : [{<contents from list above>},
{<contents from list above>}
]
}
我正在尝试这个
for key in properties.keys():
print key
json['properties'].append(property[key])
我收到此错误,
> json['properties'].append(properties[key])
KeyError: 'properties'
我在这里做错了什么?抱歉刚开始学习。谢谢
解决方案
使用字典更新方法。
json.update(properties)
例子:
properties = {
'livenessTests': [
{'name':'http' + '_' + 'livenesstest',
'testObject':'/default.html'
},
{
'name':'https' + '_' + 'livenesstest',
'testObject':'/default.html'
}
]}
json = {"acg": {
"id": "1-7KLGU.G19717",
"name": "Akamai Internal-1-7KLGU - 1-7KLGU.G19717"
},
"asmappings": [],
"cidrMaps": [],
"livenessTests" : []
}
json.update(properties)
print json
推荐阅读
- sql - 我想保留 SQL Server 表中的最新日期
- php - 为什么 mysqli_connect 不尊重 MYSQLI_OPT_CONNECT_TIMEOUT?
- javascript - Luxon 将 UTC iso 时间转换为特定时区时间(以毫秒为单位)
- python - Python Dataframe:在每个电话号码前附加“0”
- sql - 在插入前触发以将 x 天数添加到日期
- python - Python 上的 Traceroute 实现
- python - 带有 Anaconda 环境的 PyInstaller 不打包 Pip 模块
- javascript - 当 checkResolution 条件满足时,图像文件不会被清除
- html - 为什么我的 SVG 路径比我的 SVG 视口大?
- node.js - 为什么我在尝试使用 Management API 修补 Auth0 app_metadata 时收到错误 401?