简体   繁体   中英

Add item to dictionary which has same value in list of dictionary

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?

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))

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