首页 > 解决方案 > Get Dictionary Keys From NumPy Array and Values From Increment

问题描述

I have a large NumPy array (with 100,000,000 elements) that has unique and sorted values. The array may be generated with inefficiently with something like this (it will eat up your memory since the array is large but my array is derived efficiently from another source).

import numpy as np

x = np.random.choice(9509413522, size=100_000_000, replace=False)  # this will take a ton of memory!
x = np.sort(x)

With this array, I want to create a Python dictionary where the keys are the unique array elements and the values are the corresponding array indices. Typically, I could do:

some_dict = {v: i for i, v in enumerate(x)}

But this takes too long for the large array (size 100,000,000 elements!) and I was hoping that there was a faster way to accomplish this.

Ultimately, I have some array y (with redundant elements) that I need to quickly convert to its corresponding value from some_dict by doing:

np.array([some_dict[k] for k in y])

# or, alternatively

np.vectorize(some_dict.get)(y)

标签: pythonarraysnumpydictionary

解决方案


推荐阅读