如何将列表转换为2d矩阵?

时间:2017-07-01 09:03:02

标签: python

我的数据采用列表形式,但为了便于访问,我需要将它们放在11行的矩阵中,称为p0-p10,4列为a,b,c,d

这是我的数据:

[
[0.078125, 0.328125, 0.328125, 0.265625], 
[0.171875, 0.390625, 0.140625, 0.296875], 
[0.109375, 0.015625, 0.015625, 0.859375], 
[0.859375, 0.015625, 0.109375, 0.015625], 
[0.953125, 0.015625, 0.015625, 0.015625], 
[0.015625, 0.015625, 0.234375, 0.734375], 
[0.046875, 0.921875, 0.015625, 0.015625], 
[0.109375, 0.421875, 0.109375, 0.359375], 
[0.140625, 0.484375, 0.359375, 0.015625], 
[0.078125, 0.296875, 0.421875, 0.203125]
]

我曾尝试过使用numpy,但是没有其他方法吗?

1 个答案:

答案 0 :(得分:0)

numpy works

示例:

a = [[0.078125, 0.328125, 0.328125, 0.265625], [0.171875, 0.390625, 0.140625, 0.296875], [0.109375, 0.015625, 0.015625, 0.859375], [0.859375, 0.015625, 0.109375, 0.015625], [0.953125, 0.015625, 0.015625, 0.015625], [0.015625, 0.015625, 0.234375, 0.734375], [0.046875, 0.921875, 0.015625, 0.015625], [0.109375, 0.421875, 0.109375, 0.359375], [0.140625, 0.484375, 0.359375, 0.015625], [0.078125, 0.296875, 0.421875, 0.203125]]
a = np.array(a)
print(a.shape)

(10,4)

a = a.reshape((4,10))
print(a.shape)

(4,10)