[파이썬] 이차원 배열에서 원하는 요소의 index 찾기

방법 1

search_str = 'X'
two_dim_arr = [['O', 'O', 'O'], ['O', 'O', 'X'], ['O', 'O', 'O']]

answer=[(i, j) for i in range(len(two_dim_arr)) for j in range(len(two_dim_arr[0])) if two_dim_arr[i][j] == search_str][0]
print(answer)
# -> (1, 2)

방법 2

search_str = 'X'
two_dim_arr = [['O', 'O', 'O'], ['O', 'O', 'X'], ['O', 'O', 'O']]

for i, row in enumerate(two_dim_arr):
    for j, item in enumerate(row):
        if item == search_str:
            answer = (i, j)
            print(answer)
            # -> (1, 2)

links

social