我试图找出有多少快照被删除的卷。在此方案中,有一个卷 v-fffff ,其快照可用但卷已删除。我不知道怎么能找到它。以下是代码
volList=[{"VolumeId":"vol-sss","State":"in-use"},{"VolumeId":"vol-defghi","State":"available"},{"VolumeId":"vol-sfjfrf","State":"in-use"}]
snapList=[{"VolumeId":"vol-sss","snap-id":"sna-1356"},{"VolumeId":"vol-sss","snap-id":"sna-asd"},{"VolumeId":"vol-defghi","snap-id":"snap-1256"},{"VolumeId":"vol-defghi","snap-id":"snap-11"},{"VolumeId":"vol-sfjfrf","snap-id":"snap-456"},{"VolumeId":"v-fffff","snap-id":"snap-123"}]
for snap in snapList:
for vol in volList:
if snap['VolumeId'] == vol['VolumeId']:
print "match volume id :" + snap['VolumeId'] + " state " + vol['State'] + " snap-id : " + snap['snap-id']
else:
print "not match volume id :" + snap['VolumeId'] + " state not found" + " snap-id : " + snap['snap-id']
答案 0 :(得分:0)
我找到了解决方案。索引是这种情况的解决方案
volList=[{"VolumeId":"vol-sss","State":"in-use"},{"VolumeId":"vol-defghi","State":"available"},{"VolumeId":"vol-sfjfrf","State":"in-use"}]
snapList=[{"VolumeId":"vol-sss","snap-id":"sna-1356"},{"VolumeId":"vol-sss","snap-id":"sna-asd"},{"VolumeId":"vol-defghi","snap-id":"snap-1256"},{"VolumeId":"vol-defghi","snap-id":"snap-11"},{"VolumeId":"vol-sfjfrf","snap-id":"snap-456"},{"VolumeId":"v-fffff","snap-id":"snap-123"}]
print len(snapList)
volIdList=[]
for ids in volList:
volIdList.append(ids['VolumeId'])
mainSnap=[]
for snap in snapList:
try:
if (volIdList.index(snap['VolumeId'])< 0):
print " not match volume id :" + snap['VolumeId']
else:
for v in volList:
if v['VolumeId']==snap['VolumeId']:
print "match volume id :" + snap['VolumeId'] + " " + v['State'] + " " + snap['snap-id']
except ValueError:
print " state not found " + snap['VolumeId'] + " " + snap['snap-id']