OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

map::lower_bound() equivalent for python's dict class?

  • Thread starter Thread starter Homunculus Reticulli
  • Start date Start date
H

Homunculus Reticulli

Guest
I am writing some code that requires me to fetch the lower bound of a key (for simplicity, ignore keys that lie below the smallest key in the collection).

In C++, using std::map (as the most comparable data type) I would simply use the lower_bound() to return the iterator.

My Pythonfoo is not that great, but I am guessing that (in case Python does not already have a way of doing this), this would be a good use of a lambda function ...

What is the Pythonic way of retrieving the lower bound key for a given index?

In case the question is too abstract, this is what I am actually trying to do:

I have a Python dict indexed by date. I want to be able to use a date to look up the dict, and return the value associated with the lowerbound of the specified key.

Snippet follows:

Code:
mymap = { datetime.date(2007, 1, 5): 'foo',
          datetime.date(2007, 1, 10): 'foofoo',
          datetime.date(2007, 2, 2): 'foobar',
          datetime.date(2007, 2, 7): 'foobarbar' }

mydate = datetime.date(2007, 1, 7)

# fetch lbound key for mydate from mymap
def mymap_lbound_key(orig):
    pass # return the lbound for the key

I don't really want to loop through the keys, looking for the first key <= provided key, unless there is no better alternative ...
<p>I am writing some code that requires me to fetch the lower bound of a key (for simplicity, ignore keys that lie below the smallest key in the collection).</p>

<p>In C++, using std::map (as the most comparable data type) I would simply use the lower_bound() to return the iterator.</p>

<p>My Pythonfoo is not that great, but I am guessing that (in case Python does not already have a way of doing this), this would be a good use of a lambda function ...</p>

<p>What is the Pythonic way of retrieving the lower bound key for a given index?</p>

<p>In case the question is too abstract, this is what I am actually trying to do:</p>

<p>I have a Python dict indexed by date. I want to be able to use a date to look up the dict, and return the value associated with the lowerbound of the specified key.</p>

<p>Snippet follows:</p>

<pre><code>mymap = { datetime.date(2007, 1, 5): 'foo',
datetime.date(2007, 1, 10): 'foofoo',
datetime.date(2007, 2, 2): 'foobar',
datetime.date(2007, 2, 7): 'foobarbar' }

mydate = datetime.date(2007, 1, 7)

# fetch lbound key for mydate from mymap
def mymap_lbound_key(orig):
pass # return the lbound for the key
</code></pre>

<p>I don't really want to loop through the keys, looking for the first key <= provided key, unless there is no better alternative ...</p>
 

Latest posts

U
Replies
0
Views
1
user3658366
U
G
Replies
0
Views
1
Giampaolo Levorato
G
M
Replies
0
Views
1
Marcelo Rodrigo Nascimento
M
Top