I have an object class that I use as a key into a python dictionary.
from dataclasses import dataclass
@dataclass
class CompWord:
c: str
length: int
def __hash__(self):
return hash(self.c)
def __eq__(self, other):
return self.c == other
_ = lambda: defaultdict(_)
d = _()
for compword1 in [CompWord('a', 1), CompWord('b', 2)]:
key = d[compword1]
for compword2 in [CompWord('c', 1), CompWord('d', 2)]:
key = key[compword2]
at this point, d is
CompWord(c='a', length=1)
CompWord(c='c', length=1)
CompWord(c='d', length=2)
CompWord(c='b', length=2)
CompWord(c='c', length=1)
CompWord(c='d', length=2)
I want to update the key in this dictionary if I come across it again only if the length of CompWord is greater than length of key that is already in the dictionary. For example, CompWord(a, 4) > CompWord(a, 1), so I want to update this key with CompWord(a, 4), while also preserving the nested dictionaries under CompWord(a, 1).
Expected output after I update CompWord(a, 1) to Compword(a, 4)
c_word = CompWord('a', 4)
if c_word in d:
# update d[c_word] with new key CompWord(c_word.length, <old length 1>)
CompWord(c='a', length=4)
CompWord(c='c', length=1)
CompWord(c='d', length=2)
CompWord(c='b', length=2)
CompWord(c='c', length=1)
CompWord(c='d', length=2)
I need to able to reference the length of the old key above which is 1, but I am not sure how I get access to it.