在python中将平面列表读入多维数组/矩阵 [英] Read flat list into multidimensional array/matrix in python

查看:112
本文介绍了在python中将平面列表读入多维数组/矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数字列表,它们表示另一个程序生成的矩阵或数组的展平输出,我知道原始数组的尺寸,并想将这些数字读回到列表列表或NumPy矩阵中.原始数组中可能有2个以上的维度.

I have a list of numbers that represent the flattened output of a matrix or array produced by another program, I know the dimensions of the original array and want to read the numbers back into either a list of lists or a NumPy matrix. There could be more than 2 dimensions in the original array.

例如

data = [0, 2, 7, 6, 3, 1, 4, 5]
shape = (2,4)
print some_func(data, shape)

会产生:

[[0,2,7,6], [3,1,4,5]]

[[0,2,7,6], [3,1,4,5]]

提前欢呼

推荐答案

使用如果要避免将其复制到内存中,还可以直接将其分配给datashape属性:

You can also assign directly to the shape attribute of data if you want to avoid copying it in memory:

>>> data.shape = shape

这篇关于在python中将平面列表读入多维数组/矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆