Create a Dictionary-like Hashmap Structure using Numpy

Viewed 147

Is it possible to create a structure like a Python dictionary but using numpy arrays inside it?

For example, if we have 2 numpy.ndarray named keys and vals, how can we use them to create a class NumpyHashmap that will behave like a regular Python dictionary but is backed by numpy.ndarrays?

The aim for doing this is to have a more memory efficient structure than Python dictionaries. NumpyHashmap can be immutable.

import numpy as np

keys = np.array([1, 13, 24, 35, 92], dtype=np.uint16)
vals = np.array([1.234, 2.345, 3.456, 4.567, 5.678], dtype=np.float32)

# How to create a numpy-backed dictionary/hashmap?
hashmap = NumpyHashmap(keys, vals)
print(hashmap[13])      # 2.345

0 Answers
Related