The li below is a two-dimensional array, representing a matrix of six rows and three columns, and I would like to determine that the second column in the second to fifth rows is all zero.
li = [[1,1,1],
[2,0,1],
[3,0,1],
[4,0,1],
[5,0,1],
[6,1,1]]
count = 0
row1 = 2
row2 = 5
col=2
for i in range (row1-1, row2):
if li[i][col-1] == 0:count+=1
if count==row2+1-row1:
print("All 0")
The results will come out just in case, but is there a better way?
Thank you for your cooperation.
if all([item[col-1]==0 for item in li [row1-1:row2]]):
print("All 0")
Is that so?Prerequisites are row2>=row1
.
© 2024 OneMinuteCode. All rights reserved.