在python中将列表转换为2D矩阵

时间:2013-07-03 13:45:18

标签: python numpy

arr2=[0]*(x^2) # x is the length of the list data
for i in range(x):
    arr2[i]=data[i].split(',')#data is a list like:['1,2','3,4']
arr2=np.array(arr2)
A=np.asmatrix(arr2)
print A.I

设置带序列

的数组元素时出错

1 个答案:

答案 0 :(得分:0)

这样的事情:

>>> data = ['1,2','3,4']
>>> arr2=[ map(float,x.split(',')) for x in data]
>>> arr2 = np.asarray(arr2)
>>> A = np.asmatrix(arr2)
>>> A.I
matrix([[-2. ,  1. ],
        [ 1.5, -0.5]])