繁体   English   中英

在 Python 中附加多个包含 numpy 数组的列表

[英]Appending multiple lists containing numpy array in Python

我想 append 包含 numpy 数组的多个列表。 我尝试使用append但它没有 append 不同t的两个列表。 我介绍了当前和预期的输出。

import numpy as np
N=2
arsigma=[]
for t in range(0,2):
    sigma=0.02109*t*np.ones((2*N*(N+1), 1))
    arsigma.append(sigma)
    arsigma=list(sigma)
    print("sigma =",[sigma])

当前的 output 是

sigma = [array([[0.],
       [0.],
       [0.],
       [0.],
       [0.],
       [0.],
       [0.],
       [0.],
       [0.],
       [0.],
       [0.],
       [0.]])]
sigma = [array([[0.02109],
       [0.02109],
       [0.02109],
       [0.02109],
       [0.02109],
       [0.02109],
       [0.02109],
       [0.02109],
       [0.02109],
       [0.02109],
       [0.02109],
       [0.02109]])]

预期的 output 是

sigma=[array([[0.],
           [0.],
           [0.],
           [0.],
           [0.],
           [0.],
           [0.],
           [0.],
           [0.],
           [0.],
           [0.],
           [0.]]), array([[0.02109],
           [0.02109],
           [0.02109],
           [0.02109],
           [0.02109],
           [0.02109],
           [0.02109],
           [0.02109],
           [0.02109],
           [0.02109],
           [0.02109],
           [0.02109]])]

改为使用列表推导:

N = 2
arsigma = [0.02109*t*np.ones((2*N*(N+1), 1)) for t in range(2)]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM