Determining Elements in Python's Two-Dimensional Matrix

Asked 2 years ago, Updated 2 years ago, 25 views

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.

python

2022-09-30 21:11

1 Answers

 if all([item[col-1]==0 for item in li [row1-1:row2]]):
  print("All 0")

Is that so?Prerequisites are row2>=row1.


2022-09-30 21:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.