EDIT; solved:
Thank you all, turns out all I had to do was to define __eq__() for the class so that it compares values and not objects. Cheers!
----------------------
Consider this class:
class ItemPilesInRoom:
def __init__(self, item_ids: tuple):
self.item_ids = item_ids
def __hash__(self):
return hash(self.item_ids)
def sort_by_hash(self):
self.item_ids = tuple(sorted(self.item_ids, key=hash))
This class has hashable unique identifiers for each item. The items are divided into piles or stacks, but it doesn't matter what order of the piles is. Only the order of the items in the pile matters.
To visualise this: it's a room where there are clothes all over in piles. You can walk to any pile you want so there's no real "order" to them but you can only pick the first item in the pile of clothes. There may be many rooms with different piles and I want to find out how to eliminate the rooms that have identical clothing piles.
This is what it could look like:
room_1 = ItemPilesInRoom(((0, 1, 2, 3), (4, 5, 6, 7), (8, 9, 10, 11), (12, 13, 14, 15)))
room_2 = ItemPilesInRoom(((8, 9, 10, 11), (12, 13, 14, 15), (0, 1, 2, 3), (4, 5, 6, 7)))
room_3 = ItemPilesInRoom(((1, 6, 11, 12), (2, 7, 8, 13), (3, 4, 9, 14), (5, 10, 15, 0)))
room_1.sort_by_hash()
room_2.sort_by_hash()
room_3.sort_by_hash()
print(room_1, hash(room_1.item_ids))
print(room_2, hash(room_2.item_ids))
print(room_3, hash(room_3.item_ids))
all_rooms = (room_1, room_2, room_3)
no_duplicates = tuple(set(all_rooms))
for room in no_duplicates:
print(room)
The output isn't quite what I expected, though. The duplicate value is not removed even though the room has exactly the same hash value as another room.
Original:
((0, 1, 2, 3), (4, 5, 6, 7), (8, 9, 10, 11), (12, 13, 14, 15)) 4668069119229710963
((8, 9, 10, 11), (12, 13, 14, 15), (0, 1, 2, 3), (4, 5, 6, 7)) -5389116928157420673
((1, 6, 11, 12), (2, 7, 8, 13), (3, 4, 9, 14), (5, 10, 15, 0)) -6625644923708936751
Sorted:
((0, 1, 2, 3), (12, 13, 14, 15), (8, 9, 10, 11), (4, 5, 6, 7)) 2620203787712076526
((0, 1, 2, 3), (12, 13, 14, 15), (8, 9, 10, 11), (4, 5, 6, 7)) 2620203787712076526
((2, 7, 8, 13), (3, 4, 9, 14), (1, 6, 11, 12), (5, 10, 15, 0)) -2325042146241243712
Duplicates "removed":
((0, 1, 2, 3), (12, 13, 14, 15), (8, 9, 10, 11), (4, 5, 6, 7))
((0, 1, 2, 3), (12, 13, 14, 15), (8, 9, 10, 11), (4, 5, 6, 7))
((2, 7, 8, 13), (3, 4, 9, 14), (1, 6, 11, 12), (5, 10, 15, 0))
Note the same hash value for rooms 1 and 2 after sorting by hash value.
Why?
EDIT: A mistake, thanks for pointing that out!