我有以下代码行。我想在info中获取堆栈名称(标记为stack
)。我还在学习阶段。我只想要前2个循环(for
和if
)并且在info
我需要一个单独的循环,我循环通过其值在列表中的字典。我将这些值与volume id(我从1st volume.id
循环获得的for
)进行比较,当我得到匹配时,我将该字典的键放到stack
。我尝试的是下面但这是不对的,因为我没有必要循环,我从第一个循环得到重复卷的长列表。
任何人都可以让我知道如何在stack
下的一行中循环遍历字典列表值的元素?如果我需要提供更多信息,请告诉我。
for volume in volumes:
if volume.snapshot_id == snapshot_id:
for key,value in tempDict.items():
for elem in value:
if volume.id == elem:
stack = key
info={'type':'volume',
'id':volume.id,
'display_name':volume.display_name,
'snapshot_id':volume.snapshot_id,
'created_at':volume.created_at,
'snapshots':[],
'stack': stack}# This is where I need to work
vol_list.append(info)}
我在下面尝试stack
,但我还需要添加if volume.id == elem
,如果匹配则key
进入stack
stack: {k:[elem for elem in v] for k,v in tempDict.iteritems()}}
我的字典如下所示
{
"deployCI2": [
"094fd196-20f0-4e8d-b946-f74a56d2f319",
"6a1ce382-98c6-4058-a929-95a7d2415fd0",
"156a1409-c89d-48fe-af6a-cad12d985b7a",
"7376f485-6f70-4e35-a16e-b3136523206b"
],
"deployCI3": [
"c8fff661-4482-4908-b722-4fac0227a8b0",
"929cf1fa-3fa6-4f95-8464-d58e5490f4cf",
"4c7a7755-8576-43d0-9e7a-ae7319f40f6d",
"a8460ed5-913b-4670-a1d4-43af508c8187"
],
"Lomka": [
"17efb1f1-3bf2-44f0-97a1-ebca4b6a2a30"
],
"HeenaStackXYZ": [],
"deployCI4": [
"9f8ffa3c-460d-43a9-8113-58e891340e1b",
"6e535e92-4da2-4228-a6ab-c8fc8d31adcd",
"8e26a35e-7fb9-43b3-8026-d1283f7b678c",
"f40e5c29-b4df-4cfb-9d7f-3bcc9c4dcf9f"
],
"HeenaStackXYZ-VM1-ne4rpss4bnft": [],
"ci_a3077f8b": [
"0956d6a3-87f4-43c3-a2f7-8a963b2d00c3"
]
}
输出应该是什么样的?实际上这将是HTML。我主要担心的是获取堆栈名称。我做了什么
1. Fetch the stack names and the ID of the volumes associated with the stack. You see that in my dictionary.
2. I compare all the volumes in my environment and see if any volume id matches with the stack's volume ID. If yes, I need to grab that stack name and put that in the info section (That's where stack comes)
答案 0 :(得分:0)
I tried reversing the dictionary and for the stack I added - 'stack':{value for key, value in reversedDict.items() if volume.id == key}}
This worked for me.