Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop over list of lists given list of indices in tuple form

I have a list of tuples like

a = [(1, 2), (2, 2), (3, 2)]

the length of this list will vary. The order may also be inverted eg:

a = [(3, 2), (2, 2), (1, 2)]

I also have a list of lists like:

b = [['0', '0', '0'], ['0', '0', '0'], ['0', '0', '0'], ['0', '0', '0']]

Now I would like to loop over this list of lists and modify items which have the indexes:

b[3][2]
b[2][2]
b[1][2]

So the indexes depend on a. I have tried this:

for item in indexes_list:
    b[item[0]][item[1]] = '1'

But this modifies the whole last column for me (I only want to modify the last 3 items of the last column with the indices given in a). What is the best way to accomplish this?

EDIT: thanks for the responses but they have not worked for me. i have tried both and the same thing happens. i have also made that mistake on indexes_list (it was a typo) because i tried to simplify the problem. i should have elaborated further.

the list of lists is for a tic tac toe board of some sort. i have to test whether a move that is input by the user sandwiches an opponent piece. it doesn't have to be a 3x3 board, therefore long moves are possible so that multiple opponent pieces can be sandwiched. if the move does sandwich an opponent piece, the opponent piece is obliterated, to be replaced by the user piece.

when i try both methods, it quickly does it for the whole column after the first time it goes over the for loop (by putting a breakpoint and debugging, this was detected). here is my prog's code:

    for i, j in (indexes[0])[:len(y_p.rstrip('.'))]:
            print item
            board[i][j] = user
            print board

I used the print statement to see what happens while debugging. User is a variable that holds a str value of the user piece. The rstrip method is used to detect up to where the move should be.

By default, board is:

[['.', '.', '.', '.', '.'], ['.', 'X', 'O', '.', '.'], ['.', 'O', 'X', '.', '.'], ['.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.']]

y_p prints

'.OX.' # board[3][1] + board[2][1] + board[1][1] + board[0][1]

indexes[0] prints

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

but i only want to do it for

[(3, 1), (2, 1), (1, 1)]

because the board[0][1] coordinate defines an empty space (invalid). The coordinate board[3][1] which is the first in this list is where the user wants to make his move. board[2][1] is an opponent piece and board[1][1] is a user piece. therefore it should turn the pieces defined by the coordinates

[(3, 1), (2, 1), (1, 1)]

to the user's piece. But it does it for these coordinates instead (note that this is a 5x5 board)

[(4,1), (3, 1), (2, 1), (1, 1)]

so even board[4][1] gets modified even though its not in the initial indices list. After executing, board becomes:

[['.', 'X', '.', '.', '.'], ['.', 'X', 'O', '.', '.'], ['.', 'O', 'X', '.', '.'], ['.', 'X', '.', '.', '.'], ['.', 'X', '.', '.', '.']]

which isn't right (the 'X' at the first and last rows shouldn't be there)

like image 910
user1397215 Avatar asked Dec 27 '22 00:12

user1397215


1 Answers

One problem is that your list of indexes is called a but you refer to indexes_list.

Fix this error and it works: (ideone)

indexes_list = [(3, 2), (2, 2), (1, 2)]
b = [['0', '0', '0'], ['0', '0', '0'], ['0', '0', '0'], ['0', '0', '0']]

for item in indexes_list:
    b[item[0]][item[1]] = '1'

print b

Result:

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

Note: before you say "But I tried it and it doesn't work for me", please read the code very carefully and note in particular how b is constructed. The following very similar alternatives do not work because they create a list containing 4 references to the same list.

Wrong way to do it: (ideone)

bb = ['0', '0', '0']
b = [bb, bb, bb, bb]          # Wrong!!!

Another wrong way to do it: (ideone)

b = [['0', '0', '0']] * 4     # Wrong!!!
like image 158
Mark Byers Avatar answered Dec 29 '22 15:12

Mark Byers