Python 错误消息:包含多个元素的数组的真值不明确。使用a.any()或a.all()

Python 错误消息:包含多个元素的数组的真值不明确。使用a.any()或a.all(),python,numpy,Python,Numpy,我的代码有问题:当我在矩阵的第I列中进行搜索时,会出现以下错误: if grades[:,i]!=-3: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() 我试着阅读关于这个问题的老文章,但不能真正理解我的问题 我正在尝试根据丹麦评分量表编制评分函数 def computeFinalGrades(grades): #loopin

我的代码有问题:当我在矩阵的第I列中进行搜索时,会出现以下错误:

if grades[:,i]!=-3:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
我试着阅读关于这个问题的老文章,但不能真正理解我的问题

我正在尝试根据丹麦评分量表编制评分函数

def computeFinalGrades(grades):
    #looping over the leth of grades
    for i in range(len(loopingrange)):
        #checking if any grade in the i-th coloumn is -3  
        if grades[:,i]==-3:
        #if a grade is equal to -3 then the output variabel gradesFinal is -3  
            gradesFinal = -3
        else:
        #looping over all grade coloumns that do not contain one or more grades of -3
            for i in range(len(loopingrange)):
              #Checking to see if any coloumn only contains 1 grade  
                if (len(grades[:,i]) == 1):
              # if a coloumn only contains 1 grade - that is also the final grade      
                    gradesFinal = grades[:,i]
               #Checking to see if coloumn contains more than 1 grade and those grades are not -3     
                elif (len(grades[:,i]) > 1):
                    #storing all the grades in a variabel - in random order
                    unsortedgrades = grades[:,i]
                    #sorting all the grades from lowest to highest
                    sortedgrades1 = np.sort(unsortedgrades)
                    #slicing the lowest grade out using indexing
                    sortedgrades = sortedgrades1[1::]
                    #finding the final grade as the mean of the n-1 grades
                    gradesFinal = np.mean(sortedgrades)
    return gradesFinal

grades[:,i]
返回数组的第i列。这意味着它是一个数组,每行有一个元素

不能使用
if grades[:,i]==-3:
,因为
grades[:,i]==-3
返回一个布尔数组,该数组不能在if语句中使用


如果您想检查该列中是否有任何分数==-3,您可以在grades[:,i]

中使用
-3。您可以给出两列的示例,其中一列等于-3,另一列不等于?为什么不按照错误消息的建议使用
any
,以及您自己的注释?numpy可以将数组与整数进行比较。但结果是一个布尔数组,不能在if语句中使用