Python dictionary - binary search for a key?

后端 未结 5 1321
长发绾君心
长发绾君心 2021-02-05 19:16

I want to write a container class that acts like a dictionary (actually derives from a dict), The keys for this structure will be dates.

When a key (i.e. date) is used t

相关标签:
5条回答
  • 2021-02-05 19:49

    The sortedcontainers module provides a SortedDict type that maintains the keys in sorted order and supports bisecting on those keys. The module is pure-Python and fast-as-C implementations with 100% test coverage and hours of stress.

    For example:

    from sortedcontainers import SortedDict
    
    sd = SortedDict((date, value) for date, value in data)
    
    # Bisect for the index of the desired key.
    index = sd.bisect('2001/01/05')
    
    # Lookup the real key at that index.
    key = sd.iloc[index]
    
    # Retrieve the value associated with that key.
    value = sd[key]
    

    Because SortedDict supports fast indexing, it's easy to look ahead or behind your key as well. SortedDict is also a MutableMapping so it should work nicely in your type system.

    0 讨论(0)
  • 2021-02-05 19:51

    Why not just maintain a sorted list from dict.keys() and search that? If you're subclassing dict you may even devise an opportunity to do a binary insert on that list when values are added.

    0 讨论(0)
  • 2021-02-05 19:52

    I'd extend a dict, and override the __getitem__ and __setitem__ method to store a sorted list of keys.

    from bisect import bisect
    
    class NearestNeighborDict(dict):
        def __init__(self):
            dict.__init__(self)
            self._keylist = []
    
        def __getitem__(self, x):
            if x in self:
                return dict.__getitem__(self, x)
    
            index = bisect(self._keylist, x)
            if index == len(self._keylist):
                raise KeyError('No next date')
    
            return dict.__getitem__(self, self._keylist[index])
    
        def __setitem__(self, x, value):
            if x not in self:
                index = bisect(self._keylist, x)
                self._keylist.insert(index, value)
    
            dict.__setitem__(self, x, value)
    

    It's true you're better off inheriting from MutableMapping, but the principle is the same, and the above code can be easily adapted.

    0 讨论(0)
  • 2021-02-05 19:53

    You really don't want to subclass dict because you can't really reuse any of its functionality. Rather, subclass the abstract base class collections.Mapping (or MutableMapping if you want to also be able to modify an instance after creation), implement the indispensable special methods for the purpose, and you'll get other dict-like methods "for free" from the ABC.

    The methods you need to code are __getitem__ (and __setitem__ and __delitem__ if you want mutability), __len__, __iter__, and __contains__.

    The bisect module of the standard library gives you all you need to implement these efficiently on top of a sorted list. For example...:

    import collections
    import bisect
    
    class MyDict(collections.Mapping):
      def __init__(self, contents):
        "contents must be a sequence of key/value pairs"
        self._list = sorted(contents)
      def __iter__(self):
        return (k for (k, _) in self._list)
      def __contains__(self, k):
        i = bisect.bisect_left(self._list, (k, None))
        return i < len(self._list) and self._list[i][0] == k
      def __len__(self):
        return len(self._list)
      def __getitem__(self, k):
        i = bisect.bisect_left(self._list, (k, None))
        if i >= len(self._list): raise KeyError(k)
        return self._list[i][1]
    

    You'll probably want to fiddle __getitem__ depending on what you want to return (or whether you want to raise) for various corner cases such as "k greater than all keys in self".

    0 讨论(0)
  • 2021-02-05 20:10

    Use the floor_key method on bintrees.RBTree: https://pypi.python.org/pypi/bintrees/2.0.1

    0 讨论(0)
提交回复
热议问题