首页 > 解决方案 > a.any() 或 a.all() 了解如何解决此问题

问题描述

我对这个错误没有任何想法,

import NumPy as np

X = np.array([[1,2,3],
              [4,5,100],
              [-1,2,100],
              [3,8,9232020]])
print(X)
print('\n Dimension of the array is:')
print(X.shape)

def getMax(X):
  this_max=X[0]
  for e in X:
    if e > this_max:
      this_max=e
  return this_max

x_max=getMax(X)
print('x_max',x_max)
print('x_max', np.max(X))

错误是“如果 e > this_max:”,它告诉我具有多个元素的数组的真值是不明确的。使用 a.any() 或 a.all(),我不知道如何修复它。

标签: pythonnumpy

解决方案


每个 e 代表 X 中的一个列表,例如 X[0] = [1,2,3]。我假设您想遍历数组中的每个列表以找到数组中所有列表的最大值。


推荐阅读