-
-
Save avdotion/a6d14b3e9066e8af08db009ad6bd4d2b to your computer and use it in GitHub Desktop.
SortedDict — Python 3 Sorted dictionaty class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from bisect import insort | |
class SortedDict(dict): | |
def __init__(self): | |
super(dict, self).__init__() | |
self.sorted_keys = list() | |
def __setitem__(self, key, value): | |
super(SortedDict, self).__setitem__(key, value) | |
insort(self.sorted_keys, key) | |
d = SortedDict() | |
d[4] = 16 | |
d[5] = 25 | |
d[3] = 9 | |
print([d[item] for item in d.sorted_keys]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment