【问题标题】:how to get all value from while loop not just 1st iteration?如何从while循环中获取所有价值而不仅仅是第一次迭代?
【发布时间】:2019-05-28 07:33:37
【问题描述】:

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

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

【问题讨论】:

  • 将您的输入和预期输出添加到问题中。而不是告诉while循环的输出,最好解释一下你在while循环中做什么。可能会有更好的方法来做同样的事情。
  • coss() 是什么?

标签: python arrays loops


【解决方案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]

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2023-04-07
      • 2016-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-20
      • 1970-01-01
      • 1970-01-01
      • 2011-09-13
      相关资源
      最近更新 更多