簡體   English   中英

根據詞典值將字典列表轉換為單獨的列表

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

我有這樣的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"
    }
  ]
}

預期產量:

{
  "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"為我提供了不同的輸出。 遍歷列表並獲取項目無法解決我的問題。 有什么建議么

您將需要兩個循環。 每個鍵一個,每個鍵每個列表值另一個。 每次迭代追加到新字典。

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

要創建字典,您需要迭代項目,然后分配給唯一鍵。

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