2

I am trying to add a key id with the same uuid.uuid4() into the inner dictionary when 'node' values are equal and a new uuid.uuid4() when a distinct uuid is found.

Let's say 2 keys ('node' in this case) have same value like-> node: 'Bangalore', so I want to generate the same ID for it and a fresh ID for every other distinct node.

This is the code I'm working on now:

import uuid
import json

node_list = [
  {
    "nodes": [
      {
         "node": "Kunal",
         "label": "PERSON"
      },
      {
         "node": "Bangalore",
         "label": "LOC"
      }
    ]
  },
  {
    "nodes": [
      {
         "node": "John",
         "label": "PERSON"
      },
      {
         "node": "Bangalore",
         "label": "LOC"
      }
    ]
  }
]

for outer_node_dict in node_list:
      for inner_dict in outer_node_dict["nodes"]:
            inner_dict['id'] = str(uuid.uuid4()) # Remember the key's value here and apply this statement somehow?

print(json.dumps(node_list, indent = True))

This is the response I want:

"[
  {
    "nodes": [
      {
         "node": "Kunal",
         "label": "PERSON",
         "id": "fbf094eb-8670-4c31-a641-4cf16c3596d1"
      },
      {
         "node": "Bangalore",
         "label": "LOC",
         "id": "24867c2a-f66a-4370-8c5d-8af5b9a25675"
      }
    ]
  },
  {
    "nodes": [
      {
         "node": "John",
         "label": "PERSON",
         "id": "5eddc375-ed3e-4f6a-81dc-3966590e8f35"
      },
      {
         "node": "Bangalore",
         "label": "LOC",
         "id": "24867c2a-f66a-4370-8c5d-8af5b9a25675"
      }
    ]
  }
]"

But currently its generating like this:

"[
 {
  "nodes": [
   {
    "node": "Kunal",
    "label": "PERSON",
    "id": "3cce6e36-9d1c-4058-a11b-2bcd0da96c83"
   },
   {
    "node": "Bangalore",
    "label": "LOC",
    "id": "4d860d3b-1835-4816-a372-050c1cc88fbb"
   }
  ]
 },
 {
  "nodes": [
   {
    "node": "John",
    "label": "PERSON",
    "id": "67fc9ba9-b591-44d4-a0ae-70503cda9dfe"
   },
   {
    "node": "Bangalore",
    "label": "LOC",
    "id": "f83025a0-7d8e-4ec8-b4a0-0bced982825f"
   }
  ]
 }
]"

How to remember key's value and apply the same ID for it in the dictionary?

Kunal Mukherjee
  • 5,775
  • 3
  • 25
  • 53

1 Answers1

3

Looks like you want the uuid to be the same for the same "node" value. So, instead of generating it, store it to a dict

node_uuids = defaultdict(lambda: uuid.uuid4())

and then, in your inner loop, instead of

inner_dict['id'] = str(uuid.uuid4())

you write

inner_dict['id'] = node_uuids[inner_dict['node']]

A complete working example is as follows:

from collections import defaultdict

import uuid
import json

node_list = [
  {
    "nodes": [
      {
         "node": "Kunal",
         "label": "PERSON"
      },
      {
         "node": "Bangalore",
         "label": "LOC"
      }
    ]
  },
  {
    "nodes": [
      {
         "node": "John",
         "label": "PERSON"
      },
      {
         "node": "Bangalore",
         "label": "LOC"
      }
    ]
  }
]

node_uuids = defaultdict(lambda: uuid.uuid4())

for outer_node_dict in node_list:
      for inner_dict in outer_node_dict["nodes"]:
            inner_dict['id'] = str(node_uuids[inner_dict['node']])

print(json.dumps(node_list, indent = True))
Oluwafemi Sule
  • 36,144
  • 1
  • 56
  • 81
blue_note
  • 27,712
  • 9
  • 72
  • 90
  • 1
    `node_uuids = defaultdict(lambda: uuid.uuid4())` where does this fit in? – Kunal Mukherjee Nov 30 '18 at 14:55
  • 1
    @KunalMukherjee - check out [how does defaultdict work?](https://stackoverflow.com/questions/5900578/how-does-collections-defaultdict-work) which should clear it up for you. – benvc Nov 30 '18 at 15:07
  • 1
    @KunalMukherjee: the `defaultdict` generates a new uuid if one is not already stored in it – blue_note Nov 30 '18 at 15:11