首页 > 解决方案 > 为什么我不能在 python 中使用'if A or B'函数得到正确的结果

问题描述

scores = [[100,90,98,88,65],[50,45,99,85,77]]

for i in range(len(scores)):
    for j in range(len(scores[0])):
        if scores[i][j] != min(scores[i]) or scores[i][j] != max(scores[i]):
            print (scores[i][j])
Result
100
90
98
88
65
50
45
99
85
77

我想删除 100(第一行的最大值)和 45(第二行的最小值)。但它不起作用我认为'或'功能有问题。但我不知道它是什么。

标签: pythonif-statement

解决方案


您还需要检查iie row 的值,您想要比较最大值i=0和最小值i=1,并且您需要通过and运算符组合这些条件,然后是or运算符。

scores = [[100,90,98,88,65],[50,45,99,85,77]]
for i in range(len(scores)):
    for j in range(len(scores[0])):
        if (i==1 and scores[i][j] != min(scores[i])) or (i==0 and scores[i][j] != max(scores[i])):
            print (scores[i][j])
            
90
98
88
65
50
99
85
77

推荐阅读