Python Forum
List of lists manipulation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List of lists manipulation
#1
Hello !
I have a list of lists which represents a 16*16 board, so there are 16 lists of 16 integers. (representing different square types, starting with only zeros)
I want to be able, for exemple if there is a 2 or a 3 in one of the list, to expend a 2 to the next squares, only if they are 0. So up, down, left and right.
So I need to put a 2 in the same list as the first 2 or 3, one the index before and one the index after, but also one on the previous list at the same index, and one on the next list, still at the same index.
I've tried something like this :
for list in board:
        for i in list:
            if i == 2 or i == 3:
                if board[board.index(list)][list.index(i)+1] == 0 :
                    board[board.index(list)][list.index(i)+1]=2
                if board[board.index(list)][list.index(i)-1] == 0 :
                    board[board.index(list)][list.index(i)-1]=2
                if board[board.index(list)-1][list.index(i)] == 0 :
                    board[board.index(list)-1][list.index(i)]=2
                if board[board.index(list)+1][list.index(i)] == 0 :
                    board[board.index(list)+1][list.index(i)]=2
                
But it doesn't seem to work correctly and I can't find out why.
Here's my list :
board = [[0 for x in range (16)] for y in range(16)]
I add a 3 in it :
board[5][2]=3
Then if I try to apply my code, it modifies board to this :
print(board)
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 2, 3, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2], [2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2], [2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
But what I want is only board[5][1], board[5][3], board[4][2] and board [6][2] to be 2.
Anyone can help me fix this ? Smile
Reply


Messages In This Thread
List of lists manipulation - by Stahlios - Apr-11-2018, 12:27 PM
RE: List of lists manipulation - by woooee - Apr-12-2018, 07:38 PM
RE: List of lists manipulation - by Stahlios - Apr-12-2018, 09:36 PM
RE: List of lists manipulation - by Stahlios - Apr-17-2018, 07:19 PM
RE: List of lists manipulation - by woooee - Apr-17-2018, 09:45 PM
RE: List of lists manipulation - by Stahlios - Apr-18-2018, 07:43 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Check if a list exists in given list of lists Daniel94 2 2,334 Apr-07-2020, 04:54 PM
Last Post: deanhystad
  Need help with List of Lists johnissa 13 6,178 Apr-22-2018, 09:29 PM
Last Post: Larz60+
  List of lists MarkLogan 3 91,028 Feb-28-2018, 11:21 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020