python - 将来自 2 个字典的值组合成一个 np.array python

标签 python arrays numpy dictionary

我有两个指令

a = {0:[1,2,3,4], 1:[5,6,7,8],...}
b = {0:[4,3,2,1], 1:[8,7,6,5],...}

我想为每个键值对创建一个np.array c,如下所示

c1 = array([[1,4],[2,3],[3,2],[4,1]])
c2 = array([[5,8],[6,7],[7,6],[8,5]])

我该怎么做?是否可以将 np.array 存储在 python 字典中,以便我可以创建单个字典 c 而不是多个数组

最佳答案

是的,您可以将 np.array 放入 Python 字典中。只需使用 dict comprehensionzip ab 的列表。

>>> a = {0:[1,2,3,4], 1:[5,6,7,8]}
>>> b = {0:[4,3,2,1], 1:[8,7,6,5]}
>>> c = {i: np.array(list(zip(a[i], b[i]))) for i in set(a) & set(b)}
>>> c
{0: array([[1, 4], [2, 3], [3, 2], [4, 1]]),
 1: array([[5, 8], [6, 7], [7, 6], [8, 5]])}

关于python - 将来自 2 个字典的值组合成一个 np.array python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38392116/

相关文章:

python - 散点图中的颜色代码数据框

正确设置路径变量后从 power-shell 运行 Python,仍然无法启动并显示卡住

python - 模块在对象内部不可用(错误 : 'function' object has no attribute 'select' )

在不使用堆栈的情况下检查带有 3 个差异括号的字符串是否平衡

python - 根据列中的多个条件删除行(使用Python)

Python幂律适用于使用ODR的数据中的上限和不对称错误

javascript - 在javascript中将另一个数组中的数组对象打印为特定格式

arrays - 数组向量的内存布局是什么?

python numpy.convolve 解决卷积积分的限制从 0 到 t 而不是 -t 到 t

python - 矢量化 numpy : check if point is inside sphere?