简体   繁体   中英

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

I have the following list in 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']

And I'm trying to convert it into a list of dictionaries following this format:

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

so far I have:

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

But I'm having trouble making all the entries have the same key #NAPP, but different elements...

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

from pprint import pprint    
pprint(out)

Prints:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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