我有以下字典串列:
data = {
"coms": [
{
"eid": "8b1fac95-edc9-43c9-a136-4b25939cedc1",
"nid": "8d769a38-a3e2-4bf4-96f4-c17999e6b5a3",
},
{
"eid": "b8b30d9f-fe5f-4eba-a1cf-56876b82aa4e",
"nid": "3265b9ac-67ee-46e4-ab40-d509e90a378b",
}
]
}
我有一個匹配的鍵(match_key)和值(nsrid):
nsrid = "b9ec3267-5b77-4f87-aeb5-cd11c28bd518"
match_nid = "8d769a38-a3e2-4bf4-96f4-c17999e6b5a3"
我想知道如何在 match_nid 與我的字典串列中的一個 nid 匹配的串列中的字典中創建一個新的鍵/值 {'nsrid:' nsrid} 對,就像這樣:
data = {
"coms": [
{
"eid": "8b1fac95-edc9-43c9-a136-4b25939cedc1",
"nid": "8d769a38-a3e2-4bf4-96f4-c17999e6b5a3",
"nsrid": "b9ec3267-5b77-4f87-aeb5-cd11c28bd518",
},
{
"eid": "b8b30d9f-fe5f-4eba-a1cf-56876b82aa4e",
"nid": "3265b9ac-67ee-46e4-ab40-d509e90a378b",
}
]
}
作為回圈機制的一部分,nsrid可能會發生變化,并且第二次圍繞nsrid可能與第二個字典nid有關,因此我需要保留之前的更新,例如:
nsrid = "3797ce00-3e50-4286-a113-38bd61030fdc"
match_nid = "3265b9ac-67ee-46e4-ab40-d509e90a378b"
data = {
"coms": [
{
"eid": "8b1fac95-edc9-43c9-a136-4b25939cedc1",
"nid": "8d769a38-a3e2-4bf4-96f4-c17999e6b5a3",
"nsrid": "b9ec3267-5b77-4f87-aeb5-cd11c28bd518",
},
{
"eid": "b8b30d9f-fe5f-4eba-a1cf-56876b82aa4e",
"nid": "3265b9ac-67ee-46e4-ab40-d509e90a378b",
"nsrid": "3797ce00-3e50-4286-a113-38bd61030fdc",
}
]
}
uj5u.com熱心網友回復:
這能解決你的問題嗎?
data = {
"coms": [
{
"eid": "8b1fac95-edc9-43c9-a136-4b25939cedc1",
"nid": "8d769a38-a3e2-4bf4-96f4-c17999e6b5a3",
},
{
"eid": "b8b30d9f-fe5f-4eba-a1cf-56876b82aa4e",
"nid": "3265b9ac-67ee-46e4-ab40-d509e90a378b",
}
]
}
nsrid = "b9ec3267-5b77-4f87-aeb5-cd11c28bd518"
match_nid = "8d769a38-a3e2-4bf4-96f4-c17999e6b5a3"
for index in range(len(data["coms"])):
if data["coms"][index]["nid"] == match_nid:
data["coms"][index]["nsrid"] = nsrid
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/484301.html
標籤:python-3.x
下一篇:如何過濾資料框的列?
