-1

How to find i, j index of given element in matrix?

#list of pairs(i, j)
coordinates = []

# find given number i
for ri, row in enumerate(matrix):
    for ei, elem in enumerate(matrix):

        # if i is found
        # then
        # add its index to list
        if i == matrix[ri][ei]:
            coordinates.append(ri, ei)
jms
  • 17
  • 1
  • 4

3 Answers3

2

First, .append() with 2 arguments isn't going to work. You probably meant to append a tuple or list consisting of ri and ei.

Also, please don't use built–in names like list for variables – that's confusing for everyone, including the interpreter.

And as for solving your problem in an efficient manner, it would be best to iterate over list (here values_sought) not in the outer–, but in the innermost loop, so as to avoid pointless checking of the same matrix coordinates multiple times, like this:

values_sought = [1, 2, 3]
matrix = [[0,1,2], [3,2,0], [1,2,3]]
coordinates = []

for row_index, row in enumerate(matrix):
    if not values_sought: break
    for column_index, value_present in enumerate(row):
        if not values_sought: break
        for value_sought_index, value_sought in enumerate(values_sought):
            if value_present == value_sought:
                coordinates.append((row_index, column_index))
                values_sought.pop(value_sought_index)
                break

Values are removed from values_sought after being found, so you may want to have a temporary list for that purpose if you need to still have these values afterwards.

Twixes
  • 136
  • 1
  • 5
  • Thanks for feedback about my about my question.I try to avoid breaking the community guidelines. – jms Oct 30 '19 at 06:39
  • how does it happen, that values_sought index isn't out of range in 3rd loop? – jms Oct 30 '19 at 15:52
  • And why break is in IF statement, if break now breaks loop after function finds first number in matrix? Its my misunderstanding, but could you explain this? – jms Oct 30 '19 at 16:07
  • `break` only breaks the loop over `values_sought`, so that the next coordinate can be checked. And thanks to that there's no problem with the length of the list being changed during iteration – there's simply a new loop for each coordinate. – Twixes Oct 30 '19 at 19:03
  • Aaa, ok, thank you for this broad explanation, now it makes sense :) – jms Oct 30 '19 at 19:33
1

There are 2 solutions:

  1. Use two break keywords as in @Vilius Klakauskas response. Here you can read more about using else with for and break statements.

or

  1. Declare a function with return as below:
def find(i): 
    for ri, row in enumerate(matrix): 
        for ei, elem in enumerate(row): 
            if i == matrix[ri][ei]: 
                return (ri, ei) 

and call it in a loop:

coordinates = []
for i in list: 
    coordinates.append(find(i))

coordinates output:

[(0, 1), (0, 2), (1, 0)]

jms
  • 17
  • 1
  • 4
Beniamin H
  • 2,048
  • 1
  • 11
  • 17
  • There is a similar question asked: https://stackoverflow.com/questions/189645/how-to-break-out-of-multiple-loops – Beniamin H Oct 29 '19 at 18:04
0

You can achieve this with an addition of break, continue and for;else

for i in list:
    for ri, row in enumerate(matrix):
        for ei, elem in enumerate(matrix):
            #breaks out of the loop if condition is met
            if i == matrix[ri][ei]:
                coordinates.append((ri, ei))
                break 
        #if no breaks occur, continue iterating over rows
        else:
            continue
        # breaks out of the loop if first occurrence found
        break