Python循环跳过每一个其他的值 [英] Python for loop skipping every other value

查看:276
本文介绍了Python循环跳过每一个其他的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在django应用程序中遇到了一个奇怪的问题,那就是for循环正在跳过每一个其他项目。我有一个返回的查询集和 list() ed迭代。这样做的目的是删除通过POST变量传递给视图的另一个列表中的项目。该视图是ajax请求,并返回需要推送到页面的JSON列表项。在下一个请求时,页面会传递已经在页面上的对象的ID列表,所以我可以从查询集中删除那些对象,并只传回新的对象。我在代码的问题部分放了几个打印语句,并发现在页面的第一个请求上,列表因为没有任何显示而进入空白页面。查询运行并返回所有在页面上显示的结果。在第二个请求中,列表进入带有所有id的页面,这就是问题发生的地方:当我遍历查询集,检查id是否在列表中时,它只遍历奇数值(被删除),并返回偶数id对象的列表,以便在页面上第二次显示。

code:

  items = list(listobj.getItems())
temp = items
printItem List:,temp
打印渲染列表:request.POST ['rendered']。split(',')
for temp:
printItem ID:,str(item.id)$ b如果str(item.id)在request.POST ['rendered']。split(',')
printRendered List:,request.POST ['rendered' ):
items.remove(item)
printRemoved Item:,item.id
printUnrendered Items:,[item for item in item]

结果:

  [02 / Aug / 2011 20:17:25]GE项目对象><项目:项目对象>,<项目:项目对象>,<项目:项目对象> <项目:项目对象><项目:项目对象>,<项目:项目对象><项目:项目对象><项目:项目对象>] 
呈现列表:[u '']
物品ID:1
呈现列表:[u'']
物品ID:2
呈现列表:[u'']
物品ID: 3
渲染列表:[u'']
编号:4
渲染列表:[u'']
编号:5
渲染列表:[u '']
物品ID:6
呈现列表:[u'']
物品ID:7
呈现列表:[u'']
物品ID: 8
渲染列表:[u'']
编号:9
渲染列表:[u'']
未渲染项目:[1,2,3,4,5 ,6,7,8,9]
[02 / Aug / 2011 20:17:25]POST / items / HTTP / 1.1200 528
Item List:[< Item:Item object> <项目:项目对象><项目:项目对象><项目:项目对象>< It项目对象><项目:项目对象>,<项目:项目对象> ;,<项目:项目对象><项目:项目对象>]
呈现列表:[u'1' ,u'2',u'3',u'4',u'5',u'6',u'7',u'8',u'9']
物品ID:1
呈现列表:[u'1',u'2',u'3',u'4',u'5',u'6',u'7',u'8',u'9 ']
已移除物品:1
物品ID:3
呈现列表:[u'1',u'2',u'3',u'4',u'5' ,u'6',u'7',u'8',u'9']
已移除物品:3
物品ID:5
已呈现物品列表:[u'1', u'2',u'3',u'4',u'5',u'6',u'7',u'8',u'9']
删除的项目:5
物品ID:7
渲染列表:[u'1',u'2',u'3',u'4',u'5',u'6',u'7',u '8',u'9']
已移除物品:7
物品ID:9
已呈现列表:[u'1',u'2',u'3',u' 4',u'5',u'6',u'7',u'8',u'9']
删除项目:9
Unrendered Items:[2,4,6,8]
[02 / Aug / 2011 20:17:55]POST / items / HTTP / 1.1200 252


解决方案

temp 引用同一个对象,所以当你执行 items.remove()时,你也在修改 temp 。您可能需要执行 temp = items [:] 来复制项目列表的值。


I have run into an odd problem in my django application where a for loop is skipping every other item. I have take a returned queryset and list()ed to iterate over. 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. This view is an ajax request and returns a JSON list of items needed to be pushed to the page. 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. 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.

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. You probably want to do temp = items[:] to copy the values of the items list.

这篇关于Python循环跳过每一个其他的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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