Skip to content Skip to sidebar Skip to footer

Python Remove Array Items Inside For Loop

I am trying to remove some items inside an array based on the distance from each other. I have 104 items in an array with tuples: points = [(910, 28), (914, 29), (919, 30), (915, 3

Solution 1:

Removing list items in place while iterating the list is not good practice. For instance,

if points has only 3 elements [a, b, c], 
a is adjacent to b, 
b is adjacent to c, 
but a isnot adjacent to c, 
Correct result is [a] after the algorithm runs. But with your algorithm,
after the first iteration, b is removed; points = [a, c]
c can never be removed, since a and c are not adjacent.

What you are looking for is a Union-find-set, here is a implementation: http://code.activestate.com/recipes/215912-union-find-data-structure/

And here is a more naive implementation of Union-Find, for your reference: A set union find algorithm

Post a Comment for "Python Remove Array Items Inside For Loop"