I have a dictionary:
varpr = {'values': ['pr', 'tas'],
'names': ['Precipitation [mm]', 'Temperature [C deg]']}
How can I select names field if values == pr?
I expect something like x = 'Precipitation [mm]'
T try:
var = 'pr'
[v for k,v in varpr.items() if k == 'values' and v == 'pr']
but got null.
Try below to get required output:
[k for k,v in zip(varpr['names'], varpr['values']) if v == 'pr']
# ['Precipitation [mm]']
Possibly more appropriate is to define a new, restructured dictionary. Then just query the dictionary:
d = dict(zip(varpr['values'], varpr['names']))
print(d)
# {'pr': 'Precipitation [mm]', 'tas': 'Temperature [C deg]'}
print(d['pr'])
# Precipitation [mm]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With