ValueError:list.remove(x):x不在列表中,item肯定存在

时间:2013-08-15 09:27:45

标签: python

它正确删除了第一个循环中的所有内容,但在第二个循环中删除快照时将其抛出,任何想法为什么?我可能一直在运行python 3,现在又回到了2.7,这可能是我的问题吗?它似乎从列表中删除了一个项目,然后再也无法删除。

for i in range(0, numOfSnap):
            tempName = baseVv.name + ".ro" + str(i)
            snapVv = baseVv.createSv(name=baseVv.name + ".ro" + str(i), svOptions="-ro -i %s" % x)  
            x += 1                                      
            dlog.info("Creating %s" % tempName)
            baseVv.name + ".rw" + str(i)
            tempName = baseVv.name + ".rw" + str(i)
            rwSnap = snapVv.createSv(name=baseVv.name + ".rw" + str(i), svOptions="-i %s" % x)
            x += 1 
            snapRwVlun = self.cluster.createVlun(vv=rwSnap, host=host)
            dlog.info("Creating %s" % tempName)
            rwSnapList.append(rwSnap)
            vlunList.append(snapRwVlun)
            ## delay random seconds between RO snapshot creation
            time.sleep(random.randint(5,30))

        snapRoList = baseVv.snapSvList

        dlog.info("Stopping IO on VV")
        self.cluster.stopSnapIOOnHosts()

        dlog.info("Starting IO on snaps and VV")           
        self.cluster.startSnapIOOnHosts()
        dlog.info("Sleep 60 seconds for IO to go through")

        time.sleep(60)

        dlog.info("Stopping IO")
        self.cluster.stopSnapIOOnHosts()                       

        time.sleep(60)

        for vluns in vlunList:
            vluns.remove()

        dlog.info("Waiting 60 seconds")    
        time.sleep(60)

        ## randomly pick 1 rw snapshot to delete till removal of all snapshot
        for i in range(0, numOfSnap)[:]:
            snapIndex = random.randint(0, len(rwSnapList)-1)
            dlog.info("remove %s" % rwSnapList[snapIndex].name)
            roSnapshot =  rwSnapList[snapIndex]
            roSnapshot.remove()
            #self.cluster.waitForClusterIntegrated()
            ## remove it from object list
            rwSnapList.remove(roSnapshot)
            time.sleep(random.randint(5,10))

        ## randomly pick 1 ro snapshot to delete till removal of all snapshot
        for i in range(0, numOfSnap)[:]:
            snapIndex = random.randint(0, len(snapRoList)-1)
            dlog.info("remove %s" % snapRoList[snapIndex].name)
            roSnapshot =  snapRoList[snapIndex]
            roSnapshot.remove()
            #self.cluster.waitForClusterIntegrated()
            ## remove it from object list
            snapRoList.remove(roSnapshot)
            time.sleep(random.randint(5,10))

2 个答案:

答案 0 :(得分:2)

不是从快照列表中选择随机索引,而是随机列表一次并从该混洗列表中弹出项目:

shuffledSnapList = snapRoList[:]
random.shuffle(shuffledSnapList)

while shuffledSnapList:
    roSnapshot = shuffledSnapList.pop()

通过对要删除的列表使用while循环测试,可以保证循环在列表为空时终止。

因为列表是混洗的,所以项目是按随机顺序处理的。

答案 1 :(得分:0)

为什么你会首先从列表中删除它们,你只想在所有机器上调用.remove()

def removeAll(snapshots, minsleep=5, maxsleep=10):
    random.shuffle(snapshots)
    for snapshot in snapshots:
        dlg.info("remove %s" % snapshot.name)
        snapshot.remove()
        time.sleep(random.randint(minsleep, maxsleep))

removeAll(rwSnapList)
removeAll(roSnapList)