[英]Python - While-Loop until list is empty
I'm working with Django and I have a queryset of objects that has been converted to a list ( unpaid_sales
). 我正在使用Django,我有一个已转换为列表的对象的查询集(
unpaid_sales
)。 I'm executing a process that iterates through this list and operates on each item until either the list is empty, or a given integer ( bucket
) reaches zero. 我正在执行一个遍历此列表并对每个项目进行操作的进程,直到列表为空或给定的整数(
bucket
)达到零。
This is how I set it up: 我就是这样设置的:
while unpaid_sales:
while bucket > 0:
unpaid_sale = unpaid_sales.pop(0)
...do stuff
In some cases, I am getting the following error: 在某些情况下,我收到以下错误:
pop from empty list
从空列表中弹出
What's wrong with my logic? 我的逻辑出了什么问题?
Do not use separate while
loops. 不要使用单独的
while
循环。 Do as follows : 做如下:
while unpaid_sales and bucket > 0 :
unpaid_sale = unpaid_sales.pop(0)
...do stuff
Your end criteria must be formulated a little differently: loop while there are items and the bucket
is positive. 您的最终标准必须略有不同:循环同时有项目且
bucket
是积极的。 or
is not the right operation here. or
这不是正确的操作。
while unpaid_sales and bucket > 0
unpaid_sale = unpaid_sales.pop(0)
#do stuff
You should do a single loop: while bucket>0 and unpaid_sales
. 你应该做一个循环:
while bucket>0 and unpaid_sales
。 Here, you are popping elements in the bucket
loop, and then just just check that bucket
is positive, but you do not check that element_sales
still has elements in it. 在这里,您在
bucket
循环中弹出元素,然后只是检查该bucket
是否为正,但是您没有检查element_sales
仍然包含元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.