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

2009-03-28 Thread Paul Rubin
Dennis Lee Bieber wlfr...@ix.netcom.com writes: And what real difference is that going to gain versus the existing .get() method where the default is a sentinel? It's just less ugly. I don't know a way to get a unique sentinel other than sentinel = object() or something like that,

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

2009-03-27 Thread Paul Rubin
Dennis Lee Bieber wlfr...@ix.netcom.com writes: ... v = theDict.get(x, NOT_RELEVANT) ... if v is not NOT_RELEVANT: ... print x, v I think you'd normally do this with if x in theDict: print x, v but the OP was asking about a different problem, involving looking up

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

2009-03-27 Thread Carl Banks
On Mar 26, 11:02 pm, Paul Rubin http://phr...@nospam.invalid wrote: Dennis Lee Bieber wlfr...@ix.netcom.com writes: ...        v = theDict.get(x, NOT_RELEVANT) ...        if v is not NOT_RELEVANT: ...                print x, v I think you'd normally do this with      if x in theDict:  

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

2009-03-27 Thread Paul Rubin
Carl Banks pavlovevide...@gmail.com writes:      if x in theDict:           print x, v Where does v come from? Oops, pasted from original. Meant of course print x, theDict[x]. -- http://mail.python.org/mailman/listinfo/python-list

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

2009-03-27 Thread Carl Banks
On Mar 27, 11:20 am, Paul Rubin http://phr...@nospam.invalid wrote: Carl Banks pavlovevide...@gmail.com writes:      if x in theDict:           print x, v Where does v come from? Oops, pasted from original.  Meant of course print x, theDict[x]. You have look up x twice with that code,

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

2009-03-27 Thread Peter Otten
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.

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

2009-03-27 Thread Duncan Booth
Carl Banks pavlovevide...@gmail.com wrote: On Mar 27, 11:20 am, Paul Rubin http://phr...@nospam.invalid wrote: Carl Banks pavlovevide...@gmail.com writes:      if x in theDict:           print x, v Where does v come from? Oops, pasted from original.  Meant of course print x,

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

2009-03-27 Thread Carl Banks
On Mar 27, 1:06 pm, Duncan Booth duncan.bo...@invalid.invalid wrote: Carl Banks pavlovevide...@gmail.com wrote: On Mar 27, 11:20 am, Paul Rubin http://phr...@nospam.invalid wrote: Carl Banks pavlovevide...@gmail.com writes:      if x in theDict:           print x, v Where does v

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

2009-03-27 Thread andrew cooke
andrew cooke wrote: 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. i get the impression the original poster went away, and maybe they just wanted

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

2009-03-27 Thread Paul Rubin
Carl Banks pavlovevide...@gmail.com writes: Not necessarily: if the hash calculation for x is expensive enough the get version would still be faster. Yeah, the get version with the special marker value is just ugly IMO, as is the version with exceptions. Maybe there should be a two-value

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

2009-03-26 Thread Mudcat
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. So there are a couple

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

2009-03-26 Thread Daniel Fetchinson
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. So there are a

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

2009-03-26 Thread andrew cooke
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.

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

2009-03-26 Thread Paul Rubin
Mudcat mnati...@gmail.com writes: However I wondered if there was a way to simply use a range as a key reference somehow. Dictionaries aren't really the right structure for that. You want a tree or binary search structure of some sort. The bisect module might be helpful, but you will have to

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

2009-03-26 Thread bearophileHUGS
An interval map maybe? http://code.activestate.com/recipes/457411/ A programmer has to know the name of many data structures :-) Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

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

2009-03-26 Thread Aaron Brady
On Mar 26, 5:10 pm, andrew cooke and...@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'

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

2009-03-26 Thread Carl Banks
On Mar 26, 2:35 pm, Mudcat mnati...@gmail.com 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