I am trying to filter dictionary by value (dictionary with different data types), but got the error:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I want to get all records corresponding to 'YALE' value in my dictionary.
This is my code:
dataset = {
'timeseires': array([[
[ -5.653222, 7.39066 , 20.651941, 4.07861 ,-11.752331, -34.611312],
[ -5.653222, 7.39066 , 20.651941, 4.07861 ,-11.752331, -34.611312]
]]),
'site': array(['YALE', 'KKI'], dtype='<U8')
}
dataset = data.tolist()
def filter(pairs):
key, value = pairs
filter_key = 'site'
if key == filter_key and value == 'YALE':
return True
else:
return False
final_dic = dict(filter(filter, dataset.items()))
print(final_dic)
The expected output:
> dataset = {
> 'timeseires': array([[
> [ -5.653222, 7.39066 , 20.651941, 4.07861 ,-11.752331, -34.611312]
> ]]),
> 'site': array(['KKI'], dtype='<U8')
> }
From the output given, it seems like your task is to extract the
'timeseries'value corresponding to the index of the'YALE'.This code should do the trick:
If this wasn't what you were expecting please be more specific in the question.