Hi everybody,
I would like to implement a task but I'm not sure the best way to do it.
I have some objects each of which has an attribute that represent the object coordinates as a tuple.
I would like to find the neighbors of each object, that is the points located at the north, south, west and east in respect to each object. Let's say the object has coord (5,5), I am in looking for the objects situated at (5,7), (5,3), (3,5), (7,5).
I've tried with something like:
for object in objectList:
North = [e.color for e in objectList if (e.coord[0] == object.coord[0] and e.coord[1] == object.coord[1] + 2)]
East = [e.color for e in objectList if (e.coord[0] == object.coord[0] + 2 and e.coord[1] == object.coord[1])]
South = [e.color for e in objectList if (e.coord[0] == object.coord[0] and e.coord[1] == object.coord[1] - 2)]
West = [e.color for e in objectList if (e.coord[0] == object.coord[0] - 2 and e.coord[1] == object.coord[1])]
but with a list of 2000 objects it run incredibly slow... Is there a way to accomplish this task without all the many iterations of my poor code?
many thanks,
simone