Find indices of a value in 2d matrix

前端 未结 5 1774
闹比i
闹比i 2020-12-10 07:35

I have a matrix of the form,

mymatrix=[[1,2,3],[4,5,6],[7,8,9]]

I want to the get the index of, say for example, 9 which is at (2,2).

5条回答
  •  长情又很酷
    2020-12-10 08:12

    if you want to find the index of all the occurrence of a val or character in a 2d list, this code may help you and it's readable. Tnq.

    for i, e in enumerate(board):
        for j, ee in enumerate(e):
            if 'd' in ee:
                print(i, j)
    

    you can find multiple occurrences too.

提交回复
热议问题