python - append() 的问题

标签 python numpy

我正在尝试使用 x.append() 将 Python 中的不同数组添加到空列表 x 中。这就是我所做的:

x = []
y = np.zeros(2)
for i in range(3):
    y += 1
    x.append(y)

x
[array([3., 3.]), array([3., 3.]), array([3., 3.])]

如您所见,问题是它重复了最后一个结果,而我想要的是获得一个包含不同数组的列表,例如:[[3., 3.],[4., 4 .], [5., 5.]].

最佳答案

您在整个循环中更改同一个数组,将 y 的创建移动到您的循环中:

x=[]
for i in range(3):
   y = np.zeros(2) + i
   x.append(y)

关于python - append() 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52343611/

相关文章:

python - 初学者 Python : Reading and writing to the same file

python - 修改矩阵行和列的每两个最大元素

python - matplotlib 历史() : weights should have the same shape as x while shape is the same

python - 增加 Seaborn 联合图中的文本大小

python - 分段变换

python - cv2.imshow 和 numpy.dstack 核心转储

python - 如何与子进程共享父进程的 numpy 随机状态?

python - 如何将累积数据转换为每日数据?

python - 隐藏python代码

python - pandas 可以自动从 CSV 文件中读取日期吗?