【发布时间】:2017-06-23 10:13:10
【问题描述】:
它总是返回重复值示例:
{
"0": [4886, 7051, 9612, 9613, 4895],
"1": [4886, 7051, 9612, 9613, 4895],
"2": [4886, 7051, 9612, 9613, 4895],
"3": [4886, 7051, 9612, 9613, 4895],
"4": [4886, 7051, 9612, 9613, 4895]
}
我不知道为什么计数器在嵌套循环结束时重置。它应该在每批中添加下一个产品,而不是从头开始。请告诉我如何解决它?谢谢!
counter = 0
max_number = 4
batches = {}
batch = []
batch_counter = 0
while batch_counter <= max_number:
while counter <= max_number:
batch.append(data[counter])
counter = counter+1
batches[batch_counter] = batch
batch_counter = batch_counter+1
batches = json.dumps(batches)
return HttpResponse(batches)
【问题讨论】:
-
你能给个样品
data吗? -
这是它返回的内容:{"0": [4886, 7051, 9612, 9613, 4895], "1": [4886, 7051, 9612, 9613, 4895], "2" : [4886, 7051, 9612, 9613, 4895], "3": [4886, 7051, 9612, 9613, 4895], "4": [4886, 7051, 9612, 9613, 4895]} 但我想补充下一个喜欢:{"0": [4886, 7051, 9612, 9613, 4895], "1": [next ids], "3": [next ids}
-
“计数器重置”是什么意思?除了第一行之外,您的代码中没有任何行将
counter变量设置为零。
标签: python json python-2.7 python-3.x dictionary