简体   繁体   English

根据词典值将字典列表转换为单独的列表

[英]Convert list of dictionaries to separate list based on dictionary values

I have my JSON Structure like this, I wish to create new list by checking driverKey of each dictionary inside list 我有这样的JSON结构,我希望通过检查driverKey中每个字典的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"
    }
  ]
}

Expected Output: 预期产量:

{
  "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"
    }]

}

I have tried different approach but "connectionString" inside my dictionary gives me different output. 我尝试了不同的方法,但是字典中的"connectionString"为我提供了不同的输出。 loop over list and getting item does not solve my problem. 遍历列表并获取项目无法解决我的问题。 Any Suggestions 有什么建议么

You'll need two loops. 您将需要两个循环。 One per key, and another one for each list value per key. 每个键一个,每个键每个列表值另一个。 Append to a new dictionary per iteration. 每次迭代追加到新字典。

data = {'RDBMS' : [...]}

new_data = {}
for k in data:
    for l in data[k]:
        new_data.setdefault(l["driverKey"].split()[0], []).append(l)

Alternatively, use a defaultdict : 或者,使用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)

The defaultdict is slightly more efficient for a lot of data (the dict.setdefault unnecessarily creates and returns lists regardless of whether or not it has to at each turn). defaultdict对于许多数据来说效率更高( dict.setdefault不必要地创建和返回列表,而不管它是否必须每次旋转都必须这样做)。

Based on your comments, the outer loop isn't necessary, but it's always good to write code that won't easily break when your input changes. 根据您的评论,外部循环不是必需的,但是编写在输入更改时不会轻易中断的代码总是很好的。

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

to create a dict, you have iterate items and then assign to the unique key. 要创建字典,您需要迭代项目,然后分配给唯一键。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM