這篇文章帶給大家的內容是關於python實現有序字典的詳細介紹(附程式碼),有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。
主要有兩點:
一個雙向鍊錶,用來記錄字典的鍵值的插入順序
一個鍵和鍊錶節點的映射,主要用來刪除鍵的時候,找到鍵對應的節點
python程式碼實作
class Link: __slots__ = 'prev', 'next', 'key' class OrderDict: def __init__(self): self.root = Link() self.map = {} self._node_map = {} self.root.next = self.root self.root.prev = self.root def __setitem__(self, key, value): if key in self._node_map: self.map[key] = value else: root = self.root last = root.prev link = Link() link.prev, link.next, link.key = last, root, key last.next = link root.prev = link self._node_map[key] = link self.map[key] = value def __getitem__(self, item): return self.map[item] def __delitem__(self, key): del self.map[key] link = self._node_map.pop(key) link_prev, link_next = link.prev, link.next link_prev.next, link_next.prev = link_next, link_prev link.prev, link.next = None, None def pop(self): """ LIFO :return: """ if not self._node_map: raise KeyError('dict is empty') root = self.root link = root.prev link_prev = link.prev link_prev.next = root root.prev = link_prev link.prev, link.next = None, None self._node_map.pop(link.key) return self.map.pop(link.key) def __iter__(self): root = self.root curr = root.next while curr != root: yield curr.key curr = curr.next def values(self): root = self.root curr = root.next while curr != root: yield self.map[curr.key] curr = curr.next def __str__(self): root = self.root curr = root.next out = [] while curr != root: out.append((curr.key, self.map[curr.key])) curr = curr.next return str(out) if __name__ == '__main__': d = OrderDict() d['a'] = '1' d['b'] = '2' d['c'] = 'c' print(d)
【相關推薦:python教學】
#以上是python實作有序字典的詳細介紹(附程式碼)的詳細內容。更多資訊請關注PHP中文網其他相關文章!