基于字典值将字典列表转换为单独的列表

2024-09-28 01:26:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我的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"给了我不同的输出。 循环列表和获取项目并不能解决我的问题。有什么建议吗


Tags: uidserverpostgresqldriverpwdunicodeusernamepassword
2条回答

你需要两个环。每个键一个,每个键的列表值另一个。每次迭代都附加到新字典

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'
    }]
}

要创建dict,需要迭代项,然后将其分配给unique键

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)

相关问题 更多 >

    热门问题