For循环会跳过一个python交互

时间:2019-01-16 12:50:47

标签: python python-3.x list comparison

在此代码中,一个列表中的实体通过其ID与另一实体进行比较。如果ID匹配,则将实体从MyPatrolScouts列表中删除。我的问题是跳过了代码的迭代,因此产生了错误的输出。我已经添加了许多用于调试目的的打印语句,但仍然无法弄清为什么它会跳过迭代。 代码:

print(MyBadgeScouts)
print(MyPatrolScouts)
for i in MyPatrolScouts:
    print("Im now using iterating using")
    print(i)
    for j in MyBadgeScouts:
        print(i[0])
        print("Is Compared to")
        print(j[0])
        if i[0]==j[0]:
            print("Im Deleting"+str(i))
            print(i)
            MyPatrolScouts.remove(i)
            print(MyPatrolScouts)
Remove = SelectScoutView.get_children()
print("FINAL LIST")
print(MyPatrolScouts)

收益:

[('51',), ('1952',)]
[('51', 'Rory', 'Adair'), ('1952', 'Fiona', 'Adair'), ('3525', 'Ruariri', 'OBrien')]
Im now using iterating using
('51', 'Rory', 'Adair')
51
Is Compared to
51
Im Deleting('51', 'Rory', 'Adair')
('51', 'Rory', 'Adair')
[('1952', 'Fiona', 'Adair'), ('3525', 'Ruariri', 'OBrien')]
51
Is Compared to
1952
Im now using iterating using
('3525', 'Ruariri', 'OBrien')
3525
Is Compared to
51
3525
Is Compared to
1952
FINAL LIST
[('1952', 'Fiona', 'Adair'), ('3525', 'Ruariri', 'OBrien')]

预期结果为:

MyPatrolScouts=[('3525', 'Ruariri', 'OBrien')]

实际结果是:

MyPatrolScouts=[('1952', 'Fiona', 'Adair'), ('3525', 'Ruariri', 'OBrien')]

这是因为使用('1952', 'Fiona', 'Adair')进行的迭代已已跳过,但我不确定为什么。

0 个答案:

没有答案