dictionary{} processing

309
1
Jump to solution
02-13-2019 10:15 AM
JamesCrandall
MVP Frequent Contributor

Looking for most efficient method to update a dictionary that I'm populating from a REST map service and then attempting to alter as needed.  The desired alteration is simple: duplicate each item and swap value in two of the attributes of the newly copied item.  Best way to describe is just show current vs. desired output below!

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

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
      }
    }
   ]
 } ‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

I'll leave it to you whether this is "most efficient", but it works:

import copy

new_list = []
for i in mydict['junctionMatrix']:
    i_copy = copy.deepcopy(i)
    i_copy['attributes']['juncA'] = i['attributes']['juncB']
    i_copy['attributes']['juncB'] = i['attributes']['juncA']
    new_list.append(i_copy)
mydict['junctionMatrix'] += new_list
mydict

{'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}},
  {'attributes': {'WCU': 'C1',
    'calcMethod': 'AERIAL',
    'juncA': 'CR-03.0',
    'juncB': 'S47D',
    'juncDistance': 8501.86072183}},
  {'attributes': {'WCU': 'C1',
    'calcMethod': 'AERIAL',
    'juncA': 'S47D_S',
    'juncB': 'S47B_C',
    'juncDistance': 17535.28122721}}]}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

1 Reply
DarrenWiens2
MVP Honored Contributor

I'll leave it to you whether this is "most efficient", but it works:

import copy

new_list = []
for i in mydict['junctionMatrix']:
    i_copy = copy.deepcopy(i)
    i_copy['attributes']['juncA'] = i['attributes']['juncB']
    i_copy['attributes']['juncB'] = i['attributes']['juncA']
    new_list.append(i_copy)
mydict['junctionMatrix'] += new_list
mydict

{'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}},
  {'attributes': {'WCU': 'C1',
    'calcMethod': 'AERIAL',
    'juncA': 'CR-03.0',
    'juncB': 'S47D',
    'juncDistance': 8501.86072183}},
  {'attributes': {'WCU': 'C1',
    'calcMethod': 'AERIAL',
    'juncA': 'S47D_S',
    'juncB': 'S47B_C',
    'juncDistance': 17535.28122721}}]}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍