首页 > 解决方案 > 通过重复的函数调用将数组附加到自身

问题描述

我试图通过重复调用一个函数来附加一个数组。当我将追加命令放在循环中时,这可以正常工作,但当循环调用应该执行追加的函数时则不行。

import numpy as np
test_value = 555
i = 0
j = 0
test_array = np.empty([0, 3])  

def test(test_value, i, j, test_array):
    test_temp = []
    test_temp.append(i)
    test_temp.append(j)
    test_temp.append(test_value)
    test_temp_1 = test_temp

    test_temp_2 = np.array(test_temp_1)
    test_temp_2 = np.reshape(test_temp_2, (1,3))

    test_array = np.append(test_array, test_temp_2, axis=0)

    return test_array

for i in range(0,10):
    i = i + 1
    j = j + 2
    test(test_value, i, j, test_array)

print ("test array", test_array)

理想情况下,test_array 每次循环时都会添加一个新行,但是 test_array 的最终打印保持为空。

干杯

标签: numpy

解决方案


推荐阅读