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

Do not use list. Keep your objects in a dictionary indexed by coordinate tuple.

Do you have to find up and down as well? If North is (5,5), and you are using a dictionary, you can easily look up (5,7) etc. instead of the list comprehension.
print coord_dict[(5,7)]
East = [e.color for e in objectList if (e.coord[0] == object.coord[0] + 2 and e.coord[1] == object.coord[1])]

You will add one and subtract one from the first number, and do the same for the second, giving you the four neighbors (if you don't want up or down = 6 directions). And are you sure that you want +2 instead of +1? Again, if North is (5,5) and you are looking for (5,7), what is (5,6)? Finally, if all else fails, store the neighbors coordinates in another dictionary so you can look up the original coordinate, and it will point to a list of the coordinates of all neighbors.

Do you have to find up and down as well? If North is (5,5), and you are using a dictionary, you can easily look up (5,7) etc. instead of the list comprehension.
print coord_dict[(5,7)]
East = [e.color for e in objectList if (e.coord[0] == object.coord[0] + 2 and e.coord[1] == object.coord[1])]

You will add one and subtract one from the first number, and do the same for the second, giving you the four neighbors (if you don't want up or down = 6 directions). And are you sure that you want +2 instead of +1? Again, if North is (5,5) and you are looking for (5,7), what is (5,6)? Finally, if all else fails, store the neighbors coordinates in another dictionary so you can look up the original coordinate, and it will point to a list of the coordinates of all neighbors.

yes, thanks both, I've used dictionaries and everything's gone well and very performing...

I am adding 2 instead of 1 because of graphical layout needs. Points are not really points in the plot - or you won't distinguish them well, they have a certain extension (say a square with 2x2 extension, so coordinates are always odd).

thanks a lot,
simone

Good to hear. Please mark the post as solved. If you have more problems, start a new thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.