![]() ![]() The signature is the same as regular dictionaries, but keyword arguments are not recommended because their insertion order is arbitrary. def _init_ ( * args, ** kwds ): '''Initialize an ordered dictionary. # Each link is stored as a list of length three. # The sentinel element never gets deleted (this simplifies the algorithm). # The circular doubly linked list starts and ends with a sentinel element. # The internal self._map dict maps keys to links in a doubly linked list. # Big-O running times for all methods are the same as regular dictionaries. # The inherited dict provides _getitem_, _len_, _contains_, and get. _all_ from _collections import deque, defaultdict from operator import itemgetter as _itemgetter, eq as _eq from keyword import iskeyword as _iskeyword import sys as _sys import heapq as _heapq from itertools import repeat as _repeat, chain as _chain, starmap as _starmap from itertools import imap as _imap try : from thread import get_ident as _get_ident except ImportError : from dummy_thread import get_ident as _get_ident # OrderedDict # class OrderedDict ( dict ): 'Dictionary that remembers insertion order' # An inherited dict maps keys to values. from _abcoll import * import _abcoll _all_ += _abcoll. # They should however be considered an integral part of collections.py. * namedtuple factory function for creating tuple subclasses with named fields * deque list-like container with fast appends and pops on either end * Counter dict subclass for counting hashable objects * OrderedDict dict subclass that remembers the order entries were added * defaultdict dict subclass that calls a factory function to supply missing values ''' _all_ = # For bootstrapping reasons, the collection ABCs are defined in _abcoll.py. Print('= Tests before key change finished =')Īssert list(reversed(d)) = list(reversed(keys))Īssert list(reversed(d.keys())) = list(reversed(keys))Īssert list(reversed(d.'''This module implements specialized container datatypes providing alternatives to Python's general purpose built-in containers, dict, list, set, and tuple. Return self.OrderedDictXItemsView(self, super().items()) Return self.OrderedDictXKeysView(self, super().keys()) Yield self._odx._keys_int2ext.get(k, k), v # employ this trick: make it such that it appears as if k_ext has also been renamed to kįor (k, v), (k_other, v_other) in zip(ems(), ems()):įor k, v in OrderedDict(*args, **kwargs).items():ĭef _init_(self, odx: 'OrderedDictX', orig_keys):ĭef _init_(self, odx: 'OrderedDictX', orig_items): # If the key exists in the internal state but was renamed to a k_ext, Return super()._setitem_(self._keys_ext2int, v) Return super()._getitem_(self._keys_ext2int.get(k, k)) # Intentionally raise KeyError in ext2int # Check if new key matches the internal key # Change the key using internal dicts mechanism Raise Exception(f'Cannot rename key already in dict') # Validate that the old key is part of the dict Here it is: from collections import OrderedDict Please comment if you see issues or have improvements to propose as this might still be error-prone. as they appear externally to the user) to the ones in the underlying OrderedDict ("internal"). It uses 2 extra dictionaries to remap the changed keys ("external" - i.e. I was not satisfied with the dictionary needing reconstruction (at least partially), obviously for efficiency reasons, so I've put together a class that extends OrderedDict and allows you to do key changes efficiently. ![]()
0 Comments
Leave a Reply. |