I am trying to print row and column index in list.
I've used (loop below) this but Its one or the other..
a = [[0,0,1],
[0,1,0],
[1,0,0]]
def a():
x = 0
for sol in solutions:
print(sol)
for row in sol:
print(row)
I am trying to print
(0,2) (1,1) (2,0)
Index of 1s
Thank You
You can use enumerate
to generate indices for a list:
for row, sublist in enumerate(a):
for column, item in enumerate(sublist):
if item:
print((row, column))
This outputs:
(0, 2)
(1, 1)
(2, 0)
If you like numpy you can turn it into a numpy array and use argwhere()
import numpy as np
a = [[0,0,1],
[0,1,0],
[1,0,0]]
a = np.array(a)
answer = np.argwhere(a==1)
This will output:
[[0 2]
[1 1]
[2 0]]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.