Any way to use a range as a key in a dictionary?
castironpi at gmail.com
Fri Mar 27 03:18:57 CET 2009
On Mar 26, 5:10 pm, "andrew cooke" <and... at acooke.org> wrote:
> 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).
> 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.
I wonder if there is a faster method than a binary search, or what the
proof that there isn't is.
More information about the Python-list