我已经完成了我的阅读信任我。当从列表中删除一个项目时,python仍然会运行。 我有一个图形实现。python从列表中删除元素,同时遍历它
现在有一个函数应该删除节点附加的一些边。我有边缘对象列表,并想值删除它们......
代码是这样的:
for edge in node.edgeList:
...
edgeToRemove = edge #edgeToRemove now holds something like <edge.Edge object at 0x107dcaf90>
node.edgeList.remove(edgeToRemove) #KINDA WORKS - just doesnt behave consistently...It removes some edges but not others
- >什么是删除它们的最好方法?
这一定是它!有关于此的任何文档? – kosta5 2012-02-28 18:15:02
另一种方法是使用'reversed()'以相反的顺序遍历列表。这样,删除一个项目只会改变你已经看到的项目。 – kindall 2012-02-28 18:18:27
@ user965847,[yes](http://docs.python.org/tutorial/controlflow.html#for-statements)。 – senderle 2012-02-28 18:18:32