我要尝试的是遍历此字典,仅提取其中带有“ RM”的键(包括其值)。
我尝试过类似的事情:
for key, val in release_dict["packages"].items():
for k, v in val.items():
print(item for item in v["linked_jiras"] if "RM" in item)
一点运气都没有。
我被困在这里
{
"packages": {
"company-web-package-name": {
"linked_jiras": {
"RM-2048": "info we want",
"RM-2087": "info we want",
"item": "description",
"item": "description",
"item": "description",
"item": "description",
"item": "description",
"item": "description",
"item": "description",
"item": "description",
"item": "description",
},
"branch": "master",
"project_lead": "person"
},
"company-web-package": {
"linked_jiras": {
"RM-1880": "info we want",
"item": "description",
"item": "description",
"item": "description",
"item": "description",
"item": "description",
"item": "description",
"item": "description",
"item": "description",
"item": "description",
},
"branch": "master",
"project_lead": "person"
},
"company-web-package--internal": {
"linked_jiras": {
"RM-2049": "info we want",
"item": "description",
"item": "description",
"item": "description",
"item": "description",
"item": "description",
"item": "description",
"item": "description",
"item": "description",
"item": "description",
},
"branch": "master",
"project_lead": "person"
}
}
}
我正在尝试获取这些结果的新字典,或更简单地说是这些值的元组。
答案 0 :(得分:2)
您需要稍微重新排列循环并添加到输出字典中以获取所有信息:
d = {}
for key, val in release_dict["packages"].items():
for item in val["linked_jiras"]:
if "RM" in item:
d[item] = val["linked_jiras"][item]
print(d)
# {'RM-2048': 'info we want', 'RM-2087': 'info we want', 'RM-1880': 'info we want', 'RM-2049': 'info we want'}
注意 :请确保如果使用的是字典,则所有'RM'
键都是不同的。