我有这样的JSON结构,我希望通过检查列表中每个字典的driverKey
来创建新列表
{
"RDBMS": [
{
"userName": "a",
"entityName": "entity1",
"connectionString": "DRIVER={PostgreSQL Unicode};DATABASE=database1;UID=uid1;PWD=password;SERVER=127.0.0.1;SID=null;PORT=5432;",
"databaseName": "database1",
"driverKey": "PostgreSQL Unicode"
},
{
"userName": "b",
"entityName": "entity2",
"connectionString": "DRIVER={PostgreSQL Unicode};DATABASE=database1;UID=uid1;PWD=password;SERVER=127.0.0.1;SID=null;PORT=5432;",
"databaseName": "database1",
"driverKey": "PostgreSQL Unicode"
},
{
"userName": "a",
"entityName": "entity3",
"connectionString": "DRIVER={PostgreSQL Unicode};DATABASE=database1;UID=uid1;PWD=password;SERVER=127.0.0.1;SID=null;PORT=5432;",
"databaseName": "database1",
"driverKey": "SQL Server"
}
]
}
预期产出:
{
"PostgreSQL": [
{
"userName": "a",
"entityName": "entity1",
"connectionString": "DRIVER={PostgreSQL Unicode};DATABASE=database1;UID=uid1;PWD=password;SERVER=127.0.0.1;SID=null;PORT=5432;",
"databaseName": "database1",
"driverKey": "PostgreSQL Unicode"
},
{
"userName": "b",
"entityName": "entity2",
"connectionString": "DRIVER={PostgreSQL Unicode};DATABASE=database1;UID=uid1;PWD=password;SERVER=127.0.0.1;SID=null;PORT=5432;",
"databaseName": "database1",
"driverKey": "PostgreSQL Unicode"
}
],
"SQL SERVER": [
{
"userName": "a",
"entityName": "entity1",
"connectionString": "DRIVER={PostgreSQL Unicode};DATABASE=database1;UID=uid1;PWD=password;SERVER=127.0.0.1;SID=null;PORT=5432;",
"databaseName": "database1",
"driverKey": "PostgreSQL Unicode"
}]
}
我尝试了不同的方法,但我字典中的"connectionString"
给了我不同的输出。
循环列表和获取项目不能解决我的问题。任何建议
答案 0 :(得分:3)
你需要两个循环。每个密钥一个,每个密钥的每个列表值另一个密钥。每次迭代附加到新词典。
data = {'RDBMS' : [...]}
new_data = {}
for k in data:
for l in data[k]:
new_data.setdefault(l["driverKey"].split()[0], []).append(l)
或者,使用defaultdict
:
from collections import defaultdict
new_data = defaultdict(list)
for k in data:
for l in data[k]:
new_data[l["driverKey"].split()[0]].append(l)
对于大量数据,defaultdict的效率稍高(dict.setdefault
不必要地创建并返回列表,无论每次都是否必须这样做。
根据您的评论,外部循环不是必需的,但编写在输入更改时不会轻易破解的代码总是好的。
print(new_data)
{
'PostgreSQL': [{
'userName': 'a',
'entityName': 'entity1',
'connectionString': 'DRIVER={PostgreSQL Unicode};DATABASE=database1;UID=uid1;PWD=password;SERVER=127.0.0.1;SID=null;PORT=5432;',
'databaseName': 'database1',
'driverKey': 'PostgreSQL Unicode'
}, {
'userName': 'b',
'entityName': 'entity2',
'connectionString': 'DRIVER={PostgreSQL Unicode};DATABASE=database1;UID=uid1;PWD=password;SERVER=127.0.0.1;SID=null;PORT=5432;',
'databaseName': 'database1',
'driverKey': 'PostgreSQL Unicode'
}],
'SQL': [{
'userName': 'a',
'entityName': 'entity3',
'connectionString': 'DRIVER={PostgreSQL Unicode};DATABASE=database1;UID=uid1;PWD=password;SERVER=127.0.0.1;SID=null;PORT=5432;',
'databaseName': 'database1',
'driverKey': 'SQL Server'
}]
}
答案 1 :(得分:1)
创建一个dict,你有迭代项目,然后分配给唯一的密钥。
v = your_dict
output = {}
for l in v["RDBMS"]:
# check if key exists. if not create a list, to store multiple items
if l["driverKey"] not in output:
output[l["driverKey"]] = []
output[l["driverKey"]].append(l)