在for循环期间从Python列表中删除内容

在for循环期间从Python列表中删除内容,python,list,loops,Python,List,Loops,这是我的密码: toBe =[] #Check if there is any get request if request.GET.items() != []: for x in DB: try: #This is removing the PMT that is spcific to the name if request.GET['pmtName'] != "None": if not

这是我的密码:

toBe =[]
#Check if there is any get request
if request.GET.items() != []:
    for x in DB:

        try:
            #This is removing the PMT that is spcific to the name
            if request.GET['pmtName'] != "None":
                if not request.GET['pmtName'] in x['tags']:
                    print x['title'], x['tags']
                    toBe.append(x)
                    continue

            #This is removing the peak stuff
            if int(request.GET['peakMin']):
                if int(request.GET['peakMin']) < int(x['charge_peak']):
                    toBe.append(x)
                    continue
            if int(request.GET['peakMax']):
                if int(request.GET['peakMax']) > int(x['charge_peak']):
                    toBe.append(x)
                    continue
            if int(request.GET['widthMin']):
                if int(request.GET['widthMin']) < int(x['charge_width']):
                    toBe.append(x)
                    continue
            if int(request.GET['widthMax']):
                if int(request.GET['widthMax']) > int(x['charge_width']):
                    toBe.append(x)
                    continue
        except:
            pass
#TODO: Stupid hack, this needs to be fixed
for x in toBe:
    DB.remove(x)
del toBe
toBe=[]
#检查是否有任何get请求
if request.GET.items()!=[]:
对于以DB表示的x:
尝试:
#这将删除与名称相关的PMT
如果请求,则获取['pmtName']!=“无”:
如果不是请求,则在x['tags']中获取['pmtName']:
打印x['title',x['tags']
toBe.append(x)
持续
#这是删除峰值的东西
if int(request.GET['peakMin']):
如果int(request.GET['peakMin'])int(x['charge\u peak']):
toBe.append(x)
持续
if int(request.GET['widthMin']):
如果int(request.GET['widthMin'])int(x['charge\u width']):
toBe.append(x)
持续
除:
通过
#TODO:愚蠢的黑客,这需要修复
对于x in toBe:
DB.remove(x)
德尔托比
基本上,我想删除该项,然后跳到下一项。问题是,当这种情况发生时,它会打乱列表的顺序,并跳过一些。有人知道这方面的工作吗?或者只是一种不同的方式


感谢

在DB[:]中输入x:
制作列表的副本DB,这样您可以在修改原始列表的同时对其进行迭代。小心——记忆密集且缓慢

更好的方法是在列表上创建另一层,只生成部分值,然后在以后需要时迭代该层。您可以使用发电机执行此操作:

def db_view( DB ):
    for x in DB:

        #This is removing the PMT that is spcific to the name
        if request.GET.get( 'pmtName', None ) not in x['tags']:
                print x['title'], x['tags']
                continue

        #This is removing the peak stuff
        if int(request.GET['peakMin']):
            if int(request.GET['peakMin']) < int(x['charge_peak']):
                continue

        if int(request.GET['peakMax']):
            if int(request.GET['peakMax']) > int(x['charge_peak']):
                continue

        if int(request.GET['widthMin']):
            if int(request.GET['widthMin']) < int(x['charge_width']):
                continue

        if int(request.GET['widthMax']):
            if int(request.GET['widthMax']) > int(x['charge_width']):
                continue

        yield x

我通常看到的关于这类问题的答案是,如果要向后循环列表的话。以下是我在我的一个程序中如何做到这一点:

for i in range(len(my_list)-1,-1,-1):
    # do something

即使我将项目添加到列表中,这也有效。他们说你可以用“for i in list[::-1]:”来代替。我没有尝试过这样做。

您正在运行相同的
请求插值。为
x的每个值获取
。相反,您可以一次性构建一个可重用的过滤函数列表

例如:

if request.GET:
    filters = []
    if 'pmtName' in request.GET:
        n = request.GET['pmtName']
        filters.append(lambda x: n not in x['tags'])
    if 'peakMin' in request.GET and request.GET['peakMin'].isdigit():
        n = int(request.GET['peakMin'])
        filters.append(lambda x: n < int(x['charge_peak']))
    if 'peakMax' in request.GET and request.GET['peakMax'].isdigit():
        n = int(request.GET['peakMax'])
        filters.append(lambda x: n > int(x['charge_peak']))
    if 'widthMin' in request.GET and request.GET['widthMin'].isdigit():
        n = int(request.GET['widthMin'])
        filters.append(lambda x: n < int(x['charge_width']))
    if 'widthMax' in request.GET and request.GET['widthMax'].isdigit():
        n = int(request.GET['widthMax'])
        filters.append(lambda x: n > int(x['charge_width']))
或者创建一个生成器,该生成器将返回所有过滤器失效时的DB值:

filtered_DB = ( x for x in DB if all(not f(x) for f in filters))
remove_these = [ x for x in DB if any(f(x) for f in filters)]
for item in remove_these:
    DB.remove(item)
filtered_DB = ( x for x in DB if all(not f(x) for f in filters))