Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将列表转换为二维矩阵?_Python - Fatal编程技术网

Python 如何将列表转换为二维矩阵?

Python 如何将列表转换为二维矩阵?,python,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,

我的数据以列表的形式存在,但为了便于访问,我需要将它们放在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
,但它不起作用。还有其他方法吗?

numpy有效

例如:

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)


(4,10)

NumPy肯定会起作用。我建议你再试一次。你的列表在Python中已经是一个2d矩阵了,只有行和列没有名称,它们有索引。例如,您想要调用的
p1.c
包含在
数据[1][2]
中,这是列表的第二行,第三列。如果我得到我的矩阵(10,4),如何单独调用矩阵的元素?a[row_number,column namber]-对于单个元素,例如:a[2,3]
a = a.reshape((4,10))
print(a.shape)