Python Forum
List of lists manipulation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List of lists manipulation
#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


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,276 Apr-07-2020, 04:54 PM
Last Post: deanhystad
  Need help with List of Lists johnissa 13 6,059 Apr-22-2018, 09:29 PM
Last Post: Larz60+
  List of lists MarkLogan 3 82,218 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