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
#2
First, descriptive variable names help you understand what the code is doing. And using i, l, or O as single digit variable names can be confusing as the can look like numbers.
import pprint

def change_list(board):
    for row_num in range(16):
        row=board[row_num]
        for column in range(16):
            if row[column] == 3:
                ## column before
                if column-1 >= 0 and board[row_num][column-1]==0:
                    board[row_num][column-1]=3
                ## column after
                if column+1 < 16 and board[row_num][column+1]==0:
                    board[row_num][column+1]=3
                ## row above
                if row_num-1 >= 0 and board[row_num-1][column]==0:
                    board[row_num-1][column]=3
                ## row below
                if row_num+1 < 16 and board[row_num+1][column]==0:
                    board[row_num+1][column]=3

                ## if you continue on the program will find the column+1 and their neighbors will 
                ## be changed, and then the row+1 will be found, etc. until the end of the list
                return board

board = [[0 for x in range (16)] for y in range(16)]
board[5][2]=3
board=change_list(board)
pprint.pprint(board)
Reply
#3
Exactly what I needed, thank you very much for the help :)
Reply
#4
Hello again there, in fact I still have little problem.
Either I get the program to do it over and over until basically the whole list is full, or I manage to get it only working on the first "3" he finds on the list and stops there.
But if for exemple when I use it there are more than one "3" in the list, I want the squares next to each initial three to change, so not only the first one it finds, but not to continue doing it on the newly added "3s".
Sorry if it's just me not understanding it properly or if I'm not explaining myself correctly tho, but thanks to any future answer
Reply
#5
1. Go through the list and store the location of each square you want to change 2. go through that list, or whatever you store the locations in, and pass each location to be changed to the function that does the changing.
Reply
#6
Yup I guess I'll try something like that. Thanks !
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Check if a list exists in given list of lists Daniel94 2 2,204 Apr-07-2020, 04:54 PM
Last Post: deanhystad
  Need help with List of Lists johnissa 13 5,876 Apr-22-2018, 09:29 PM
Last Post: Larz60+
  List of lists MarkLogan 3 63,968 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