1

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.

1
  • No, the compiled numpy code does not have anything special for dictionaries. Commented Nov 23, 2019 at 17:20

2 Answers 2

1

You could do this with a structured array and np.in1d.

import numpy as np

d = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}

# Create structured array (modify formats, names as necessary):
names = ['key', 'data']  
formats = ['U10', 'i4']                                                                        
dtype = dict(names = names, formats = formats)

arr = np.array(list(d.items()), dtype=dtype)

# >>> arr
# array([('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)],
#       dtype=[('key', '<U10'), ('data', '<i4')])

# use np.in1d to get your keys:

keys = ['a', 'c', 'e']

values = arr[np.in1d(arr['key'], keys)]['data']                                                

# >>> values
# array([1, 3, 5], dtype=int32)
Sign up to request clarification or add additional context in comments.

1 Comment

This is incredibly convoluted and essentially pointless (turning an O(1) looking into an O(n)). I love it.
1

If you just require to have numpy arrays as input and output but don't insist on using a numpy function to handle them, you can use numpy.array() on the result of a list comprehension:

import numpy as np

d = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
keys = np.array(['a', 'c', 'e'])
values = np.array([d[k] for k in keys])

print(values)

2 Comments

A list comprehension is a streamlined for loop, but otherwise not much a change from the OP's approach.
I would still recommend to use list comprehension over append, because it is much faster.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.