Last active
February 23, 2017 18:40
-
-
Save caffeine-potent/31f3bcb2fadbb1730fc977f182dbd95c to your computer and use it in GitHub Desktop.
select numerical keys in dictionary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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