python - 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)
解决方案
推荐阅读
- java - 如何查看 com.fasterxml.jackson 日志消息
- javascript - Chart.js -> 折线图 -> 具有相同 X 的多个点
- chisel - 如何从凿子中破译生成的 Verilog 中的注释?
- r - R - visNetwork - 向边缘添加动画
- meteor - 使用流星流-db-admin 的 simpl-schema 无法与 autoform 一起使用
- python-3.x - Pyspark:如何在通过配置单元上下文执行时在 sql 脚本中传递参数
- https - 谷歌云 ssl 证书“无法解析 SSL 证书”
- node.js - 使用 Promise all 进行异步调用
- sqoop - 如何确保使用 sqoop 导入的数据在 hive 中遵循与 mysql 表中相同的行顺序
- c# - 如何在 ExcelDataReader 中强制执行无类型列?