JavaScript
x
10
10
1
def check(A,B,a,b):
2
return A >= a and B >= b
3
4
A = [500, 500, 500]
5
a = [20, 0, 0]
6
B = [5, 5, 5, 5]
7
b = [0, 10, 0, 0]
8
9
print(check(A, B, a, b))
10
(result True)
I want result become False if only one in list A or B have small value than a or b. In this code B[1] < b[1] (5 >= 10) result I expect is False but is output True
Advertisement
Answer
From official doc:
The comparison uses lexicographical ordering: first the first two items are compared, and if they differ this determines the outcome of the comparison; if they are equal, the next two items are compared, and so on, until either sequence is exhausted.
To get False
in your example, you can use built-in all()
to compare all values in arrays:
JavaScript
1
11
11
1
def check(A, B, a, b):
2
return all(x >= y for x, y in zip(A, a)) and all(x >= y for x, y in zip(B, b))
3
4
5
A = [500, 500, 500]
6
a = [20, 0, 0]
7
B = [5, 5, 5, 5]
8
b = [0, 10, 0, 0]
9
10
print(check(A, B, a, b))
11
Prints:
JavaScript
1
2
1
False
2