Python 添加新的 key/value 到键与值匹配的字典列表
Python add new key/value to list of dictionaries where key matches a value
我有以下词典列表:
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"
我想知道如何在列表中的字典中创建一个新的 key/value {'nsrid:' nsrid} 对,其中 match_nid 与我的字典列表中的 nid 匹配,所以像这样:
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",
}
]
}
这是否解决了您的问题?
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
我有以下词典列表:
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"
我想知道如何在列表中的字典中创建一个新的 key/value {'nsrid:' nsrid} 对,其中 match_nid 与我的字典列表中的 nid 匹配,所以像这样:
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",
}
]
}
这是否解决了您的问题?
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