简体   繁体   English

Python for循环跳过其他所有值

[英]Python for loop skipping every other value

I have run into an odd problem in my django application where a for loop is skipping every other item. 我在django应用程序中遇到了一个奇怪的问题,其中for循环正在跳过其他所有项目。 I have take a returned queryset and list() ed to iterate over. 我将返回的queryset和list()进行迭代。 The point of doing this is to remove items that are inside another list that is getting passed to the view via a POST variable. 这样做的目的是删除通过POST变量传递到视图的另一个列表中的项目。 This view is an ajax request and returns a JSON list of items needed to be pushed to the page. 该视图是ajax请求,它返回需要推送到页面的项目的JSON列表。 upon the next request, the page passes the list of IDs of objects that are already on the page, so I can remove those from the queryset and pass back only new ones. 在下一个请求时,页面将传递页面上已经存在的对象ID的列表,因此我可以将这些ID从queryset中删除,然后仅传递新的ID。 I put several print statements throughout the problem portion of the code and figured out that the on the first request from the page, the list comes into the page empty because there aren't any displayed. 我在代码的问题部分中放置了一些打印语句,并发现在页面的第一个请求中,由于没有显示任何内容,因此列表进入页面为空。 The query runs and returns all the results which then get displayed on the page. 查询将运行并返回所有结果,然后这些结果将显示在页面上。 On the second request, the list comes into the page with all of the id's, and this is where the problem occurs: As I loop through the queryset, checking to see if the id's are in the list, it only iterates over the odd values (which are removed) and returns a list of the even id'd objects to get displayed a second time on the page. 在第二个请求上,列表与所有ID一起进入页面,这就是问题所在:当我遍历查询集时,检查ID是否在列表中,它仅遍历奇数值(已删除)并返回偶数对象的列表,以便第二次在页面上显示。

code: 码:

items = list(listobj.getItems())
temp = items
print "Item List: ", temp
print "Rendered List: ", request.POST['rendered'].split(',')
for item in temp:
    print "Item ID: ", str(item.id)
    print "Rendered List: ", request.POST['rendered'].split(',')
    if str(item.id) in request.POST['rendered'].split(','):
        items.remove(item)
        print "Removed Item: ", item.id
print "Unrendered Items: ", [item.id for item in items]

Results: 结果:

    [02/Aug/2011 20:17:25] "GET /list/list HTTP/1.1" 200 6256
Item List:  [<Item: Item object>, <Item: Item object>, <Item: Item object>, <Item: Item object>, <Item: Item object>, <Item: Item object>, <Item: Item object>, <Item: Item object>, <Item: Item object>]
Rendered List:  [u'']
Item ID:  1
Rendered List:  [u'']
Item ID:  2
Rendered List:  [u'']
Item ID:  3
Rendered List:  [u'']
Item ID:  4
Rendered List:  [u'']
Item ID:  5
Rendered List:  [u'']
Item ID:  6
Rendered List:  [u'']
Item ID:  7
Rendered List:  [u'']
Item ID:  8
Rendered List:  [u'']
Item ID:  9
Rendered List:  [u'']
Unrendered Items:  [1, 2, 3, 4, 5, 6, 7, 8, 9]
[02/Aug/2011 20:17:25] "POST /items/ HTTP/1.1" 200 528
Item List:  [<Item: Item object>, <Item: Item object>, <Item: Item object>, <Item: Item object>, <Item: Item object>, <Item: Item object>, <Item: Item object>, <Item: Item object>, <Item: Item object>]
Rendered List:  [u'1', u'2', u'3', u'4', u'5', u'6', u'7', u'8', u'9']
Item ID:  1
Rendered List:  [u'1', u'2', u'3', u'4', u'5', u'6', u'7', u'8', u'9']
Removed Item:  1
Item ID:  3
Rendered List:  [u'1', u'2', u'3', u'4', u'5', u'6', u'7', u'8', u'9']
Removed Item:  3
Item ID:  5
Rendered List:  [u'1', u'2', u'3', u'4', u'5', u'6', u'7', u'8', u'9']
Removed Item:  5
Item ID:  7
Rendered List:  [u'1', u'2', u'3', u'4', u'5', u'6', u'7', u'8', u'9']
Removed Item:  7
Item ID:  9
Rendered List:  [u'1', u'2', u'3', u'4', u'5', u'6', u'7', u'8', u'9']
Removed Item:  9
Unrendered Items:  [2, 4, 6, 8]
[02/Aug/2011 20:17:55] "POST /items/ HTTP/1.1" 200 252

temp and items refer to the same object, so when you do items.remove() you're also modifying temp . tempitems指向同一个对象,因此当您执行items.remove()您也在修改temp You probably want to do temp = items[:] to copy the values of the items list. 您可能想要执行temp = items[:]复制items列表的值。

You should not be modifying a data structure while iterating over it. 在迭代数据结构时,您不应修改它。

Anyways, this is a more concise and performant code to do your operation: 无论如何,这是执行操作的更简洁,更高效的代码:

items = list(listobj.getItems())
rendered = set((int(i) for i in request.POST['rendered'].split(',')))
unrendered = [item for item in items if item.id not in rendered]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM