title: Use range as a key value in a dictionary, most efficient way?
body: <p>I have been wondering if there is some kind of data-structure or clever way to use a dictionary (O(1) lookup) to return a value if there are given values for defined ranges that do not overlap. So far I have been thinking this could be done if the ranges have some constant difference (0-2, 2-4, 4-6, etc.) or a binary-search could be done to do this in O(log(n)) time.</p>
  
  <p>So, for example given a dictionary, </p>
  
  <pre><code>d = {[0.0 - 0.1): "a",
       [0.1 - 0.3): "b",
       [0.3 - 0.55): "c",
       [0.55 - 0.7): "d",
       [0.7 - 1.0): "e"}
  </code></pre>
  
  <p>it should return,</p>
  
  <pre><code>d[0.05] 
  &gt;&gt;&gt; "a"
  d[0.8]
  &gt;&gt;&gt; "e"
  d[0.9]
  &gt;&gt;&gt; "e"
  d[random.random()] # this should also work
  </code></pre>
  
  <p>Is there anyway to achieve something like this? Thanks for any responses or answers on this.</p>