第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將項目添加到字典列表中具有相同值的字典

將項目添加到字典列表中具有相同值的字典

SMILET 2021-09-02 14:59:28
當(dāng)“節(jié)點”值相等時,我試圖將id具有相同的鍵添加uuid.uuid4()到內(nèi)部字典中,并uuid.uuid4()在找到不同的 uuid 時添加一個新鍵。假設(shè) 2 個鍵(在本例中為“節(jié)點”)具有相同的值,例如 -> 節(jié)點:“班加羅爾”,因此我想為其生成相同的 ID,并為每個其他不同的節(jié)點生成一個新 ID。這是我現(xiàn)在正在處理的代碼:import uuidimport jsonnode_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))這是我想要的回應(yīng):"[  {    "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"      }    ]  }]"但目前它的生成是這樣的:"[ {  "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"   }  ] }]"如何記住鍵的值并在字典中為其應(yīng)用相同的 ID?
查看完整描述

1 回答

?
一只斗牛犬

TA貢獻(xiàn)1784條經(jīng)驗 獲得超2個贊

看起來您希望uuid對于相同的“節(jié)點”值是相同的。因此,與其生成它,不如將其存儲到 dict


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

然后,在你的內(nèi)循環(huán)中,而不是


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

你寫


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

一個完整的工作示例如下:


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


查看完整回答
反對 回復(fù) 2021-09-02
  • 1 回答
  • 0 關(guān)注
  • 177 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號