1

So here is the pretty basic code i am working with, it is a function that takes a grid in the form of a tuple of tuples. In the loop I have tried to make i the rows and q the columns for the grid.

def myfunc(grid):  
    for i in (grid): 
        for q in i: 
            print("i.index(q): ", i.index(q), " grid.index(i)", grid.index(i))

    return True

myfunc(((1, 0, 0, 1, 0),
        (0, 1, 0, 0, 0),
        (0, 0, 1, 0, 1),
        (1, 0, 0, 0, 0),
        (0, 0, 1, 0, 0)))

Here is what I get:

i.index(q):  0  grid.index(i) 0
i.index(q):  1  grid.index(i) 0
i.index(q):  1  grid.index(i) 0
i.index(q):  0  grid.index(i) 0
i.index(q):  1  grid.index(i) 0
i.index(q):  0  grid.index(i) 1
i.index(q):  1  grid.index(i) 1
i.index(q):  0  grid.index(i) 1
i.index(q):  0  grid.index(i) 1
i.index(q):  0  grid.index(i) 1
i.index(q):  0  grid.index(i) 2
i.index(q):  0  grid.index(i) 2

and so on.

I expecting and want to get 0, 1, 2, 4, 5, ... for i.index(q), am I not using this function correctly? This seems odd as grid.index(i) is working fine and when I printed q in the for q in i: loop I got the right values.

Any help appreciated!

2 Answers 2

1

You're using them perfectly. But they're not what you want to use.

def myfunc(grid):  
    for (i, row) in enumerate(grid): 
        for (j, el) in enumerate(row): 
            print('{},{}'.format(i, j))
Sign up to request clarification or add additional context in comments.

Comments

0

tuple.index returns index of first occurrence of the given element:

In [1]: tuple.index?
Docstring:
T.index(value, [start, [stop]]) -> integer -- return first index of value.
Raises ValueError if the value is not present.
Type:      method_descriptor

In order to iterate over elements along with their indices, you should use enumerate function:

In [2]: def myfunc(grid):
   ...:     for i, row in enumerate(grid):
   ...:         for j, elem in enumerate(row):
   ...:             print('Row #{}, elem #{}, value: {}'.format(i, j, elem))

In [3]: myfunc(((1, 0, 0, 1, 0),
   ...:         (0, 1, 0, 0, 0),
   ...:         (0, 0, 1, 0, 1),
   ...:         (1, 0, 0, 0, 0),
   ...:         (0, 0, 1, 0, 0)))
Row #0, elem #0, value: 1
Row #0, elem #1, value: 0
Row #0, elem #2, value: 0
Row #0, elem #3, value: 1
Row #0, elem #4, value: 0
Row #1, elem #0, value: 0
Row #1, elem #1, value: 1
Row #1, elem #2, value: 0
Row #1, elem #3, value: 0
Row #1, elem #4, value: 0
Row #2, elem #0, value: 0
Row #2, elem #1, value: 0
Row #2, elem #2, value: 1
Row #2, elem #3, value: 0
Row #2, elem #4, value: 1
Row #3, elem #0, value: 1
Row #3, elem #1, value: 0
Row #3, elem #2, value: 0
Row #3, elem #3, value: 0
Row #3, elem #4, value: 0
Row #4, elem #0, value: 0
Row #4, elem #1, value: 0
Row #4, elem #2, value: 1
Row #4, elem #3, value: 0
Row #4, elem #4, value: 0

1 Comment

@C.Dow, do not forget to accept answer using the tick under the post's score buttons, this will help users with same problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.