如何从while循环中获取所有值而不仅仅是第一次迭代?

时间:2019-05-28 07:33:38

标签: python arrays loops

最终的是具有4行的numpy数组,我需要从嵌套的while循环中获取所有值,但是这段代码仅返回final的第一行或循环的第一迭代。

def amir2 (final):
    i = 0
    j = 0
    temp = []
    temp2 = []
    temp3 = []
    while i < len(final):
        while j < len(final):
            cos_lib = coss(final[i] , final[j])
            temp.append(cos_lib)
            j += 1
        temp2.append(temp)
        i += 1
    return temp2

2 个答案:

答案 0 :(得分:1)

您需要提供有关numpy数组final的确切格式的详细信息,以及使用coss方法在循环中要执行的操作。

如果final定义如下:

final = np.array([[1,2,3],[1,2,3],[1,2,3]], dtype=np.float64)

您可以使用.shape获取数组,因此可以如下迭代数组的所有元素:

for x in range(0, final.shape[0]):
    for y in range(0, final.shape[1]):
        print final[x, y]

答案 1 :(得分:-1)

对于numpy使用temp2 = np.append(temp2,temp)