How do I search for a specific value in a 2d array in Python?

Question:

I want to iterate through 2 specific rows of a 2d array to find a specific value, but I am unable to do so.

In my program, I have read in a 2d array from a text file using the following code:

with open ("maze_10x10.txt") as maze:
    for line in maze:
        row = [item.strip() for item in line.split(' ')]
        a.append(row)

For reference, the text file looks like this:

1 1 1 1 1 1 1 0 1 1 
1 0 1 1 0 1 1 0 1 1 
1 0 0 0 0 0 1 0 0 1 
1 1 1 1 1 0 1 1 0 1 
1 0 1 0 1 0 0 0 0 1 
1 1 1 0 1 1 0 1 1 1 
1 0 0 0 0 0 0 0 1 1 
1 0 1 1 0 1 1 0 1 1 
1 0 0 0 1 0 0 0 1 1 
1 0 1 1 1 1 1 1 1 1 

I want to find the coordinate of the only zeroes in the first and last rows of the array and I am attempting to do this with this code:

start = 9,0
end = 0,0
for i in range(len(a)):
    if a[9][i] == 0:
        start = 9,i
print(start)
for i in range(len(a)):
    if a[0][i] == 0:
        end = 0,i
print(end)

But the start and end variables remain unchanged when I run it.

Asked By: Noah

||

Answers:

I think your issue is with your if statement. You are reading a text file and comparing it to an integer. Try either adding quotes to the zeros or converting each number to an integer

for i in range(len(a)):
    if a[9][i] == "0":
        start = 9,i
Answered By: TacoWarrior24

I think it’s best to start from scratch.

Once you load the file into a, a is:

[['1', '1', '1', '1', '1', '1', '1', '0', '1', '1', ''],
 ['1', '0', '1', '1', '0', '1', '1', '0', '1', '1', ''],
 ['1', '0', '0', '0', '0', '0', '1', '0', '0', '1', ''],
 ['1', '1', '1', '1', '1', '0', '1', '1', '0', '1', ''],
 ['1', '0', '1', '0', '1', '0', '0', '0', '0', '1', ''],
 ['1', '1', '1', '0', '1', '1', '0', '1', '1', '1', ''],
 ['1', '0', '0', '0', '0', '0', '0', '0', '1', '1', ''],
 ['1', '0', '1', '1', '0', '1', '1', '0', '1', '1', ''],
 ['1', '0', '0', '0', '1', '0', '0', '0', '1', '1', ''],
 ['1', '0', '1', '1', '1', '1', '1', '1', '1', '1', '']]

First thing you want to do is filter out the empty strings ('') and turn the non-empty strings into ints. This can be achieved with a nested list comprehension.

a = [[int(number) for number in row if number != ''] for row in a]

Then, a looks something like:

[[1, 1, 1, 1, 1, 1, 1, 0, 1, 1],
 [1, 0, 1, 1, 0, 1, 1, 0, 1, 1],
 [1, 0, 0, 0, 0, 0, 1, 0, 0, 1],
 [1, 1, 1, 1, 1, 0, 1, 1, 0, 1],
 [1, 0, 1, 0, 1, 0, 0, 0, 0, 1],
 [1, 1, 1, 0, 1, 1, 0, 1, 1, 1],
 [1, 0, 0, 0, 0, 0, 0, 0, 1, 1],
 [1, 0, 1, 1, 0, 1, 1, 0, 1, 1],
 [1, 0, 0, 0, 1, 0, 0, 0, 1, 1],
 [1, 0, 1, 1, 1, 1, 1, 1, 1, 1]]

Then, you can define a function to scan a row from a list, returning the coordinates of each match.

def scan(lst, row_number, match):
    row = lst[row_number]
    matches = []
    for i, element in enumerate(row):
        if element == match:
            matches.append((row_number, i))
    return matches

Then, you can try running it for the first row:

print(scan(a, 0, 0))  # Outputs [(0, 7)]

And the last row:

print(scan(a, 9, 0))  # Outputs [(9, 1)]
Answered By: linuxsushi

If you only care about the first and last rows, you don’t need to loop over all the row indexes.

And you can use the built-in index() method to find the position of the 0 in a specific row.

start = (9, a[9].index(0))
end = (0, a[0].index(0))
Answered By: Barmar