[英]How to find list rows and columns index in Python
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) (0,2)(1,1)(2,0)
Index of 1s 1s索引
Thank You 谢谢
You can use enumerate
to generate indices for a list: 您可以使用
enumerate
为列表生成索引:
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() 如果您喜欢numpy,则可以将其转换为numpy数组,并使用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]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.