Skip to content

Instantly share code, notes, and snippets.

@caffeine-potent
Last active February 23, 2017 18:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caffeine-potent/31f3bcb2fadbb1730fc977f182dbd95c to your computer and use it in GitHub Desktop.
Save caffeine-potent/31f3bcb2fadbb1730fc977f182dbd95c to your computer and use it in GitHub Desktop.
select numerical keys in dictionary
my_dict = dict({
1 : "",
2 : "",
3 : "oyy",
"oyy" : "",
1990 : 's kid',
2000 : "a",
2014 : "b",
2015 : "c",
"foo" : "bar",
2016 : "d",
2017 : "e",
2018 : "f",
4 : ""
})
class ndict(dict):
def num_keys(self):
return filter(lambda x: isinstance(x, (int, long, float)), self.keys())
def slice_key(self, min=float("-inf"), max=float("inf")):
if (min > max):
return None
return filter(lambda x: (x >= min) & (x <= max), self.num_keys())
my_dict = ndict(my_dict)
print(my_dict.num_keys()
## >> [2016, 1, 2, 3, 4, 1990, 2017, 2018, 2000, 2014, 2015]
print(my_dict.slice_key(2014,2016))
## >> [2016, 2014, 2015]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment