简体   繁体   中英

Create dictionary from duplicates and swap values in output dictionary

Looking for most efficient method to duplicate each item in a dictionary but swap attribute values of two attributes and append to a new dictionary.

(attributes "juncA" and "juncB" must have their values swapped on copied item).

Pretty simple: take what is in "current" dictionary and produce the "desired" output.

Current:

{
  "junctionMatrix": [
    {
      "attributes": {
        "WCU": "C1",
        "calcMethod": "AERIAL",
        "juncA": "S47D",
        "juncB": "CR-03.0",
        "juncDistance": 8501.86072183
      }
    },
    {
      "attributes": {
        "WCU": "C1",
        "calcMethod": "AERIAL",
        "juncA": "S47B_C",
        "juncB": "S47D_S",
        "juncDistance": 17535.28122721
      }
    }
   ]
 } 

Desired:

{
  "junctionMatrix": [
    {
      "attributes": {
        "WCU": "C1",
        "calcMethod": "AERIAL",
        "juncA": "S47D",
        "juncB": "CR-03.0",
        "juncDistance": 8501.86072183
      }
    },
    {
      "attributes": {
      "WCU": "C1",
      "calcMethod": "AERIAL",
      "juncA": "CR-03.0",
      "juncB": "S47D",
      "juncDistance": 8501.86072183
    }
    },
    {
      "attributes": {
        "WCU": "C1",
        "calcMethod": "AERIAL",
        "juncA": "S47B_C",
        "juncB": "S47D_S",
        "juncDistance": 17535.28122721
      }
    },
    {
      "attributes": {
        "WCU": "C1",
        "calcMethod": "AERIAL",
        "juncA": "S47D_S",
        "juncB": "S47B_C",
        "juncDistance": 17535.28122721
      }
    }
   ]
 }

One way to do it is to use an auxiliary function that will create the new element and return it, so that you 'll append it on the list:

inDict= {
  "junctionMatrix": [
    {
      "attributes": {
        "WCU": "C1",
        "calcMethod": "AERIAL",
        "juncA": "S47D",
        "juncB": "CR-03.0",
        "juncDistance": 8501.86072183
      }
    },
    {
      "attributes": {
        "WCU": "C1",
        "calcMethod": "AERIAL",
        "juncA": "S47B_C",
        "juncB": "S47D_S",
        "juncDistance": 17535.28122721
      }
    }
   ]
 }

# Auxiliary function.
def swap(elem):
    newElem = {'attributes': {'WCU':elem['attributes']['WCU'], 
    'calcMethod':elem['attributes']['calcMethod'],
    'juncA':elem['attributes']['juncB'], 
    'juncB':elem['attributes']['juncA'],
    'juncDistance':elem['attributes']['juncDistance']}}
    return newElem

# Creating the new dict.
newDict = {'junctionMatrix':[]}
for index, elem in enumerate(inDict['junctionMatrix']):
    newDict['junctionMatrix'].append(elem)
    newDict['junctionMatrix'].append(swap(elem))

#Printing it.
for i in newDict['junctionMatrix']:
    print(i)

Result:

{'attributes': {'WCU': 'C1', 'calcMethod': 'AERIAL', 'juncA': 'S47D', 'juncB': 'CR-03.0', 'juncDistance': 8501.86072183}}
{'attributes': {'WCU': 'C1', 'calcMethod': 'AERIAL', 'juncA': 'CR-03.0', 'juncB': 'S47D', 'juncDistance': 8501.86072183}}
{'attributes': {'WCU': 'C1', 'calcMethod': 'AERIAL', 'juncA': 'S47B_C', 'juncB': 'S47D_S', 'juncDistance': 17535.28122721}}
{'attributes': {'WCU': 'C1', 'calcMethod': 'AERIAL', 'juncA': 'S47D_S', 'juncB': 'S47B_C', 'juncDistance': 17535.28122721}}

This also might work for you.

import copy 

demo_dict_cpy = {}
demo_dict_cpy['junctionMatrix'] = []
for each in demo_dict['junctionMatrix']:
    if 'juncA' in each['attributes'] and 'juncB' in each['attributes']:
        demo_dict_cpy['junctionMatrix'].append(each)

        each_copy = copy.deepcopy(each)
        each_copy['attributes']['juncA'] = each['attributes'].get('juncB')
        each_copy['attributes']['juncB'] = each['attributes'].get('juncA')
        demo_dict_cpy['junctionMatrix'].append(each_copy)

print(demo_dict_cpy)

Result:

{'junctionMatrix': [{'attributes': {'WCU': 'C1',
    'calcMethod': 'AERIAL',
    'juncA': 'S47D',
    'juncB': 'CR-03.0',
    'juncDistance': 8501.86072183}},
  {'attributes': {'WCU': 'C1',
    'calcMethod': 'AERIAL',
    'juncA': 'CR-03.0',
    'juncB': 'S47D',
    'juncDistance': 8501.86072183}},
  {'attributes': {'WCU': 'C1',
    'calcMethod': 'AERIAL',
    'juncA': 'S47B_C',
    'juncB': 'S47D_S',
    'juncDistance': 17535.28122721}},
  {'attributes': {'WCU': 'C1',
    'calcMethod': 'AERIAL',
    'juncA': 'S47D_S',
    'juncB': 'S47B_C',
    'juncDistance': 17535.28122721}}]}

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