Any way to use a range as a key in a dictionary?

andrew cooke andrew at acooke.org
Thu Mar 26 18:10:18 EDT 2009


Mudcat wrote:
> I would like to use a dictionary to store byte table information to
> decode some binary data. The actual number of entries won't be that
> large, at most 10. That leaves the other 65525 entries as 'reserved'
> or 'other' but still need to be somehow accounted for when
> referenced.
[...]

i don't completely follow what you are doing, but i currently use the
following to find a transition in a finite automaton for a regular
expression, and i suspect it's similar to what you want.

my problem is that i want to associate certain ranges of characters with a
particular value.  for example, a-c might be 1, p-x might be 2.  i use
intervals that are (a, b) tuples, where a and b are the inclusive bounds
(so (a, b) means a <= x <= b).

i have a class that is a collection of these intervals, called Character.


from bisect import bisect_left

class Character:
  def __init__(self, intervals):
    '''
    Note that intervals must already be sorted.
    '''
    self.__intervals = intervals
    self.__index = [interval[1] for interval in intervals]
  def __contains__(self, c):
    '''
    Does the value lie within the intervals?
    '''
    if self.__index:
      index = bisect_left(self.__index, c)
      if index < len(self.__intervals):
        (a, b) = self.__intervals[index]
        return a <= c <= b
    return False

this lets me test for whether things are in range:

>>> c = Character([('a','c'),('p','x')])
>>> 'a' in c
True
>>> 'm' in c
False

and i just realised that i deleted the code that lets me also associate
values with intervals!  but hopefully that gives you the idea.  bisect can
be used to find values via a sorted index.  so once you find "index"
above, you can use that to return a value from an array.

i doubt this is super-fast (i think the bisect library is pure python),
but it was good enough for my initial attempt.

andrew






More information about the Python-list mailing list