Reputation: 1471
Is it possible to get all keys in a dictionary with values above a threshold?
A dictionary could look like:
mydict = {(0,1,2): "16", (2,3,4): "19"}
The threshold could be 17 for example.
Upvotes: 30
Views: 56328
Reputation: 23131
One can use filter()
function here.
list(filter(lambda k: float(mydict[k]) >= 17, mydict))
Another even more convoluted way is to create boolean selectors and compress similar to how boolean indexing works in pandas/numpy.
# using built-in methods
from itertools import compress, repeat
import operator
list(compress(mydict, map(operator.ge, map(float, mydict.values()), repeat(17))))
# or using a lambda
list(compress(mydict, map(lambda x: float(x) >= 17, mydict.values())))
Speaking of pandas/numpy, if you need to filter keys by values repeatedly, pandas Series or numpy array is a useful data type to store the data in, so that this kind of filtering can be done vectorially in a more efficient way.
Using pandas Series:
import pandas as pd
my_srs = pd.Series(mydict).astype(int)
my_srs.index[my_srs >= 17].tolist()
Using numpy arrays (will have to create two arrays, one for the keys and another for the values and use the values array to filter the keys array):
import numpy as np
keys = np.array(list(mydict.keys()), dtype=object)
values = np.array(list(mydict.values()), dtype=float)
keys[values >= 17].tolist()
Using numpy structured array:
import numpy as np
arr = np.array(list(mydict.items()), dtype=[('keys', object), ('values', int)])
arr['keys'][arr['values'] >= 17]
Upvotes: 1
Reputation: 476659
Of course it is possible. We can simply write:
[k for k,v in mydict.items() if float(v) >= 17]
Or in the case you work with python-2.7, you - like @NoticeMeSenpai says - better use:
[k for k,v in mydict.iteritems() if float(v) >= 17]
This is a list comprehension. We iterate through the key-value pairs in the mydict
dictionary. Next we convert the value v
into a float(v)
and check if that float is greater than or equal to 17. If that is the case, we add the key k
to the list.
For your given mydict
, this generates:
>>> [k for k,v in mydict.items() if float(v) >= 17]
[(2, 3, 4)]
So a list containing the single key that satisfied the condition here: (2,3,4)
.
Upvotes: 60