For循环跳过迭代python

时间:2019-02-01 16:23:19

标签: python python-3.x list comparison

我正在尝试遍历我的结果,以便巡逻队长(Rory Adair)或助理巡逻队长(Fiona Adair)都不会出现在myresults列表中。  我尝试实现此代码,但是由于myresult.remove(i)导致Fiona Adair出现在myresults中,因此for循环会跳过迭代的问题。 我添加了RemoveList,以便可以在不跳过迭代的情况下将其删除,但是问题仍然存在。

OfficerList=['Rory', 'Adair', 'Fiona', 'Adair']
myresults=[('Rory', 'Adair'), ('Fiona', 'Adair'), ('Toby', 'Cheevers')]
PatrolLeaderName=(OfficerList[0],OfficerList[1])
AssistantPatrolLeader=(OfficerList[2],OfficerList[3])
RemoveList=myresults
print("Myresults")
print(RemoveList)
for i in myresults:
    print("Checking")
    print(i)
    print("Comparing")
    print((i[0],i[1]))
    if ((i[0],i[1])) == PatrolLeaderName:
        print("Removing because Scout is PL")
        print(i)
        RemoveList.remove(i)
    if ((i[0],i[1])) == AssistantPatrolLeader:
        print("Removing because Scout is APL")
        print(i)
        RemoveList.remove(i)
print("Non Officer Scouts")
print(RemoveList)
for i in RemoveList:
            #This code sends data to Treeview
    PatrolLookView.insert("", "end", values=((""), i[0], i[1]))
print("Finished")

预期输出:

RemoveList=['Toby','Cheevers']

实际输出:

RemoveList=[('Fiona', 'Adair'), ('Toby', 'Cheevers')]

感谢您的帮助

0 个答案:

没有答案