class mydict(dict):
def __getitem__(self,key):
if key in self.keys():
return self[key]
ranges = []
for k in self.keys():
start,stop = k.split('-')
ranges.append((int(start),int(stop)))
ranges.sort()
key = int(key)
for start,stop in ranges:
if key>=start and key<=stop:
return dict.__getitem__(self,"%s-%s"%(start,stop))
if __name__=='__main__':
d = mydict({'1-3':'abc','4-8':'def','9-12':'ghi'})
print '2:',d[2]
d['13-18'] = 'jkl'
print '15:',d[15]
print "'16':",d['16']
print d.keys()
print d.values()
print d
|