I have a dictionary with a bunch of integer keys. for keys I don't have, I'd like to be able to retrieve the smallest and largest keys right before and after they key I want to retrieve but that does not exist.
The Treemap class in java has two methods doing exactly this: ceilingkey()
and floorkey()
.
How can I do this with python?
As an example I have a dictionary like this:
{ 1: "1", 4: "4", 6: "6" ..., 100: "100" }
If I ask for key 1
, I'll retrieve "1"
,
but if I look for key 3
, I should get KeyError
and hence be able to get floor(3) = 1
and ceil(3) = 4
.