簡體   English   中英

如何將項目列表翻譯成字典列表,其中項目作為字典元素

[英]How to translate a list of items, into a list of dictionaries with the items as dictionary elements

我在 python 中有以下列表:

napps_list= ['84:kytos/kytos',
             '85:kytos/python-openflow',
             '88:kytos/of_core',
             '101:kytos/kytos-utils',
             '90:kytos/mef_eline',
             '91:kytos/flow_manager',
             '92:kytos/topology',
             '93:kytos/kronos',
             '94:kytos/pathfinder',
             '95:kytos/status',
             '96:kytos/of_l2ls',
             '97:kytos/of_lldp',
             '103:kytos/maintenance',
             '99:kytos/storehouse',
             '107:kytos/kytos-end-to-end-tester']

我正在嘗試將其轉換為以下格式的字典列表:

 {
    "data": [
          {
              "{#NAPP}": "kytos/mef_eline",
          },
          {
              "{#NAPP}": "kytos/pathfinder",
          },
          {
              "{#NAPP}": "kytos/topology",
          }
    ]
}

到目前為止,我有:

content = dict() 
content["data"] = [] 
content["data"]["#NAPP"] = dict()

但是我無法讓所有條目都具有相同的鍵#NAPP,但元素不同......

out = {"data": [{"{#NAPP}": i.split(":")[-1]} for i in napps_list]}

from pprint import pprint    
pprint(out)

印刷:

{'data': [{'{#NAPP}': 'kytos/kytos'},
          {'{#NAPP}': 'kytos/python-openflow'},
          {'{#NAPP}': 'kytos/of_core'},
          {'{#NAPP}': 'kytos/kytos-utils'},
          {'{#NAPP}': 'kytos/mef_eline'},
          {'{#NAPP}': 'kytos/flow_manager'},
          {'{#NAPP}': 'kytos/topology'},
          {'{#NAPP}': 'kytos/kronos'},
          {'{#NAPP}': 'kytos/pathfinder'},
          {'{#NAPP}': 'kytos/status'},
          {'{#NAPP}': 'kytos/of_l2ls'},
          {'{#NAPP}': 'kytos/of_lldp'},
          {'{#NAPP}': 'kytos/maintenance'},
          {'{#NAPP}': 'kytos/storehouse'},
          {'{#NAPP}': 'kytos/kytos-end-to-end-tester'}]}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM