Say I have a dictionary:
d = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
Given a numpy array** of keys, e.g. ['a' 'c' 'e']
, is there an easy way to create a numpy array of the corresponding values, e.g. [1 3 5]
?
** for reasons of the context I'm using this in, I'd prefer to input/output numpy arrays. Something similar can obviously be done with lists:
keys = ['a', 'c', 'e']
values = []
for k in keys:
values.append(d[k])
print(values)
[1, 3, 5]
But I am wondering if there is a built-in way to do something like this in numpy.