I've seen the related question, but I can't use a frozenset
for my inner-sets. I want everything to be mutable.
As I understand it, Python gives everything an ID, so why can't it use that as its hash? I need to maintain a list of references to all the inner-sets, even as they change.
Edit: Ok, I understand why, but in this case it would be preferable, as I only care about reference equality, not value equality.
How can I do this?
You're going to ask "why", so I'll give you some code:
def remove_captured(self):
all_chains = set()
chains = Grid(self.rows, self.cols)
for m, n, stone in self.enumerate():
if stone == self[m - 1, n]:
chains[m, n] = chains[m - 1, n]
if stone == self[m, n - 1]:
all_chains.discard(chains[m, n - 1])
chains[m, n].update(chains[m, n - 1])
for s in chains[m, n - 1]:
chains[s] = chains[m, n]
elif stone == self[m, n - 1]:
chains[m, n] = chains[m, n - 1]
else:
chains[m, n] = set()
all_chains.add(chains[m, n])
chains[m, n].add((m,n))
chains._print()
print all_chains
I've essentially got a game board, and I want to divide the pieces on the board into groups (or "chains"). The above code works fine until I added all_chains
-- it creates all the sets, but then I have no way of accessing each the sets its created without iterating over the whole board again.
So how do I maintain a list of all the sets it's created? Keep in mind that I need to remove sets too (which is why I want to use another set for the outter set).
Wrapping the reference in weakref.ref()
didn't work either:
all_chains.add(weakref.ref(chains[m, n])) # TypeError: unhashable type: 'set'