我有一个奇怪的问题。有没有人看到我的代码有什么问题?Python for循环跳过其他循环?
for x in questions:
forms.append((SectionForm(request.POST, prefix=str(x.id)),x))
print "Appended " + str(x)
for (form, question) in forms:
print "Testing " + str(question)
if form.is_valid():
forms.remove((form,question))
print "Deleted " + str(question)
a = form.save(commit=False)
a.audit = audit
a.save()
else:
flag_error = True
结果:
Appended Question 50
Appended Question 51
Appended Question 52
Testing Question 50
Deleted Question 50
Testing Question 52
Deleted Question 52
这似乎跳过问题51.被添加到列表中,但for循环跳过它。有任何想法吗?
它在哪里说,这是不允许的? – Marcin 2012-03-16 14:50:50
因此,在这种情况下,我会建议标记要删除的问题(或保留要删除的问题的索引列表),并在循环后实际删除它们。 – egor83 2012-03-16 14:52:14
好点,它不安全,但允许。 – 2012-03-16 14:52:35