I have some points in space where each point has an id. I also have a subset of these points in another group that have different id values.
How can I create a new type of id for both groups of points so that the points that have the same coordinates end up using the same id values?
I assume I need to generate hash codes using their coordinates which should give me the same id value for points that have the same coordinates, right?
I am confused how I could use it because the set of hashcodes is much smaller than float[3]. So not sure if I am on the right track.
I’m not completely sure what you mean here, but you could use
__hash__with atuple:Now, objects which contain the same point all hash to the same value. As a side benefit, they can now be used as dictionary keys or in set objects a little bit more reasonably.
Of course, if you’re going to write a class this simple, you might want to consider a
collections.namedtupleinstead. You could even subclass it (it’s all spelled out in the link). This has the advantage of the object being immutable — Mutating a hashable object just isn’t a nice thing to do ;-). The objects also have no__dict__associated with them, so they’ll probably be a little easier on your memory if you’re creating 100M of them.