python - 在 Python 中将列表列表转换为数组

标签 python arrays list numpy

我一直在查找此内容,但找不到任何有帮助的内容。我在制作所需大小的数组时遇到了一些麻烦。

到目前为止,我已经使用以下代码创建了一个列表列表:

n=4
def matrixA(k):
    A=[]
    for m in range(0,k):
        row=[]
        #A.append([])
        for n in range(0,k):
            if (n==(m+1)) or (n==(m-1)):
                row.append(-deltaX/(deltaX)**2)
            if (n==m):
                row.append(2*deltaX/(deltaX)**2)
            else:
                row.append(0)
        A.append(row)
    return A
pprint.pprint(matrixA(n))
print len(matrixA(n))

我得到这个输出。

[[128.0, -64.0, 0, 0, 0],
 [-64.0, 0, 128.0, -64.0, 0, 0],
 [0, -64.0, 0, 128.0, -64.0, 0],
 [0, 0, -64.0, 0, 128.0]]
4

现在,我想将其设为大小为 (4,4) 的数组。我的问题是,当我执行以下操作时(将列表转换为数组并尝试对其进行整形):

A=numpy.array(matrixA(n))
print A
print "This is its shape:",A.shape
A.shape=(n,n)

我得到:

[[128.0, -64.0, 0, 0, 0] [-64.0, 0, 128.0, -64.0, 0, 0]
 [0, -64.0, 0, 128.0, -64.0, 0] [0, 0, -64.0, 0, 128.0]]
This is its shape: (4,)

然后出现错误:

ValueError: total size of new array must be unchanged

我该如何从这里获取大小为 (4,4) 的数组?

最佳答案

欢迎来到 numpy 世界。

看来你想要:

array([[ 128.,  -64.,    0.,    0.],
       [ -64.,  128.,  -64.,    0.],
       [   0.,  -64.,  128.,  -64.],
       [   0.,    0.,  -64.,  128.]])

在构建列表列表时,很难考虑行和列索引。在 numpy 中,您首先形状,然后填充,这通常更容易。

一步一步:

In [37]: from numpy import eye,diag,ones # some useful functions

In [38]: eye(4) # identity matrix
Out[38]: 
array([[ 1.,  0.,  0.,  0.],
       [ 0.,  1.,  0.,  0.],
       [ 0.,  0.,  1.,  0.],
       [ 0.,  0.,  0.,  1.]])

In [39]: eye(4)*128
Out[39]: 
array([[ 128.,    0.,    0.,    0.],
       [   0.,  128.,    0.,    0.],
       [   0.,    0.,  128.,    0.],
       [   0.,    0.,    0.,  128.]])

In [40]: ones(3)
Out[40]: array([ 1.,  1.,  1.])

In [41]: diag(ones(3),1)  # see help(diag)
Out[41]: 
array([[ 0.,  1.,  0.,  0.],
       [ 0.,  0.,  1.,  0.],
       [ 0.,  0.,  0.,  1.],
       [ 0.,  0.,  0.,  0.]])

In [42]: diag(ones(3),1).T   # transpose
Out[42]: 
array([[ 0.,  0.,  0.,  0.],
       [ 1.,  0.,  0.,  0.],
       [ 0.,  1.,  0.,  0.],
       [ 0.,  0.,  1.,  0.]])

所以你想要的是这样的:

def arrayA(n,deltaX):
    A=eye(n)  # id matrix of size n 
    B= diag(ones(n-1),1) # just ahead
    return (2*A-B-B.T)*(deltaX/deltaX**2)

然后运行:

In [45]: arrayA(4,1/64)
Out[45]: 
array([[ 128.,  -64.,    0.,    0.],
       [ -64.,  128.,  -64.,    0.],
       [   0.,  -64.,  128.,  -64.],
       [   0.,    0.,  -64.,  128.]])

对于大矩阵,速度更快:

In [57]: %timeit arrayA(100,1/64)
1000 loops, best of 3: 326 µs per loop

In [58]: %timeit matrixA(100)
100 loops, best of 3: 14.9 ms per loop

关于python - 在 Python 中将列表列表转换为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36424159/

相关文章:

python - cv2.error : OpenCV(4. 5.2) .error : (-215:Assertion failed) ! _src.empty() 函数 'cv::cvtColor'

c++ - 如何从动态分配的数组中删除元素?

python - python 3.7 中 .append 函数的自动化

python - 如何使用运算符 'and' 和/或 'or' 映射两个过滤列表

Python read_text() 添加额外的字符串

python - 无法退出正在运行的 jupyter notebook 服务器

c# - 无法将 JSON 数组(例如 [1,2,3])反序列化为类型 ' ',因为类型需要 JSON 对象(例如 {"name":"value"})才能正确反序列化

c++ - 更改类型后丢失对 std::variant 对象的引用

python如何检查一个字符串是否是一个字符串列表的元素

python - 禁用 qslider 的特定键盘事件