使用del python时停止列表更新 [英] stop list updating when using del python

查看:73
本文介绍了使用del python时停止列表更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表,我想从中删除某些元素 我创建了一个for循环,并且在此for循环中放置了我的列表[i]

I have a list where i want to delete certain elements from I have created a for loop and in this for loop have put del my list[i]

prule是一个字符串列表,其中包含数字,例如从0159大小为11154

prule is a list of strings containing things like numbers as strings from 0 to 159 its size 11154

def getind(li)
    ret = {}
    for i, x in enumerate(li):
        if x not in ret:
            ret[x] = []
        ret[x].append(i)
    return ret
dups = getind(prule)
for n in dups.get('159',[]):
    del rulelines[n]

一段时间后,我猜是因为该列表已更新.有什么我可以使用的,所以直到for循环完成之前它都不会更新

After a while of doing this list assignment index out of range which I am guessing is because the list gets updated. Is there anything I can use so it doesn't get updated until the for loop is finished

推荐答案

我已在答案中更正了此处

dups = getind(prules)
indexes = dups.get('156',[])
rulelines = [rulelines[i] for i in range(len(rulelines[:])) if i not in indexes]

应该可以解决您的问题.

Should be what solves your problem.

这篇关于使用del python时停止列表更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆