( , 50, 25, 1 25), - dict, " " int.
. , list , , , , ..
defaultdict, , . A defaultdict , , len, , , . ... , defaultdict , ; , , , , ...
list -like, collections.abc.MutableSequence .
class ExpandoList(collections.abc.MutableSequence):
def __init__(self):
self.d = {}
self.maxidx = -1
def __setitem__(self, idx, value):
self.maxidx = max(idx, self.maxidx)
self.d[idx] = value
def __getitem__(self, idx):
self.maxidx = max(idx, self.maxidx)
return self.d.get(idx, 0)
def __delitem__(self, idx):
for i in range(i, self.maxidx):
self.d[i] = self.d[i-1]
self.maxidx -= 1
def insert(self, idx, value):
for i in reversed(range(i, self.maxidx)):
self.d[i] = self.d[i-1]
self.d[idx] = value
self.maxidx += 1
def __iter__(self):
yield from (self[i] for i in range(self.maxidx))
def __len__(self):
return self.maxidx
Please note that this does not cut. You can handle this manually, but it is becoming more complex than I would like to answer. If you want to take this further, I have a class lazylistthat I can load somewhere that shows how to solve the same problems that you need to solve.