Change single element in 2D list changes every 1D element - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Change single element in 2D list changes every 1D element (/thread-6261.html) |
Change single element in 2D list changes every 1D element - AceScottie - Nov-12-2017 I have a 2D list of 10x10 0s' (user can change the amount from command line) list = [ [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] ] i want to change list[x][y] to a 1 list[x][y] = 1 however the output i get is : list = [ [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0] ] code: #global vars maze = wright_permission = True #---- def set_pixel(x, y): # Changes the element in x row and y column to 1 global maze # loads the global object global wright_permission while True: if wright_permission: # for quick threading later wright_permission = False maze[x][y] = 1 wright_permission = True break def create_maze(w, h): # creates a 2D list of 0 from the 2 numbers given in the command line. mazesec = [0] * w mazearr = [mazesec] * h return mazearr def main(w, h, startx, starty): global maze # loads a global object maze = create_maze(w, h) # sets the global object to an 2D list of 0s for i in range(0, len(maze)): print maze set_pixel(startx, starty) # sets a single random element in the list to a 1 print("created new maze!") for j in range(0, len(maze)): print maze[j] if __name__ == "__main__": try: w = int(sys.argv[1]) h = int(sys.argv[2]) except: print("Error: use 'python maze.py int-width int-height' replacing int-width and int-height with a number for width and height of the maze") main(w, h, random.randint(1, w-1), random.randint(1, h-1)) RE: Chance single element in 2D list changes every 1D element - heiner55 - Nov-13-2017 What do you want ? a. some pixel should be 1 b. all pixel should be 1 RE: Chance single element in 2D list changes every 1D element - Larz60+ - Nov-13-2017 What are you passing on the command line? If would be very reassuring if you inserted a print statement of what you are sending to function main. immediately after starting main, add: if python 3.6: print(f'w: {w}, h: {h}, startx: {startx}, starty: {starty}')otherwise: print('w: {}, h: {}, startx: {}, starty: {}'.format(w, h, startx, starty)) RE: Change single element in 2D list changes every 1D element - AceScottie - Nov-13-2017 the command line is something like 'python maze.py 10 10' the numbers are just there to set the height and width of the list I need to be able to chande 1 single element in the list example output list = [ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 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] ] this would mean the values of startx and starty are both 1 so list[1][1] = 1 RE: Change single element in 2D list changes every 1D element - Larz60+ - Nov-13-2017 ' something like ' d0esn't work in programming, what was the actual input? RE: Change single element in 2D list changes every 1D element - AceScottie - Nov-13-2017 (Nov-13-2017, 05:43 PM)Larz60+ Wrote: ' something like ' d0esn't work in programming, there is nothing 'something like' about it. if the list is 10x10 elements the user (in this case me) put 10 and 10 as the variables in the command line. it wouldnt matter if it was 10 10 or 1000 1000, the size of the lists are irrelevant, my question is regarding changing a single element in a 2D list from a 0 to a 1 imagine this is a square of black pixels, i want to change 1 singular pixel to white. the size of the full image would not matter the only thing that matters is the coordinates of that one singular pixel. so my question is simple: why does 'list[x][y] = 1' change every element in the 'y'th position to a 1 for the entire list how do i change the singe element without changing any other element in the list. here is the full code: https://pastebin.com/tseM76rH RE: Change single element in 2D list changes every 1D element - Larz60+ - Nov-13-2017 Quote:' something like ' d0esn't work in programming,your words, not mine. In The code below , note that i changed the call to main for testing. # global vars maze = [] wright_permission = True # ---- def set_pixel(x, y): # Changes the element in x row and y column to 1 global maze # loads the global object global wright_permission maze[x][y] = 1 def create_maze(w, h): # creates a 2D list of 0 from the 2 numbers given in the command line. mazesec = [0] * w mazearr = [mazesec] * h return mazearr def main(w, h, startx, starty): global maze # loads a global object print(f'w: {w}, h: {h}, startx: {startx}, starty: {starty}') maze = create_maze(w, h) # sets the global object to an 2D list of 0s for i in range(0, len(maze)): print('{}'.format(maze[i])) set_pixel(startx, starty) # sets a single random element in the list to a 1 print("\ncreated new maze! x = {}, y = {}".format(startx, starty)) for j in range(0, len(maze)): print(maze[j]) if __name__ == "__main__": # try: # w = int(sys.argv[1]) # h = int(sys.argv[2]) # except: # print( # "Error: use 'python maze.py int-width int-height' replacing int-width and int-height with a number for width and height of the maze") # main(w, h, random.randint(1, w - 1), random.randint(1, h - 1)) # Don't use random while testing main(10, 10, 3, 7)result: w: 10, h: 10, startx: 3, starty: 7 [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] created new maze! x = 3, y = 7 [0, 0, 0, 0, 0, 0, 0, 1, 0, 0] [0, 0, 0, 0, 0, 0, 0, 1, 0, 0] [0, 0, 0, 0, 0, 0, 0, 1, 0, 0] [0, 0, 0, 0, 0, 0, 0, 1, 0, 0] [0, 0, 0, 0, 0, 0, 0, 1, 0, 0] [0, 0, 0, 0, 0, 0, 0, 1, 0, 0] [0, 0, 0, 0, 0, 0, 0, 1, 0, 0] [0, 0, 0, 0, 0, 0, 0, 1, 0, 0] [0, 0, 0, 0, 0, 0, 0, 1, 0, 0] [0, 0, 0, 0, 0, 0, 0, 1, 0, 0] RE: Change single element in 2D list changes every 1D element - Larz60+ - Nov-13-2017 Use this one: # global vars import copy maze = wright_permission = True # ---- def set_pixel(x, y, maze): # Changes the element in x row and y column to 1 global wright_permission maze[x][y] = 1 return maze def create_maze(w, h): # creates a 2D list of 0 from the 2 numbers given in the command line. maze = [[0 for x in range(w)] for y in range(h)] return maze def main(w, h, startx, starty): # global maze # loads a global object # print(f'w: {w}, h: {h}, startx: {startx}, starty: {starty}') maze = create_maze(w, h) # sets the global object to an 2D list of 0s for i in range(0, len(maze)): print('{}'.format(maze[i])) maze = set_pixel(startx, starty, maze) # sets a single random element in the list to a 1 print("\ncreated new maze! x = {}, y = {}".format(startx, starty)) for j in range(0, len(maze)): print(maze[j]) if __name__ == "__main__": # try: # w = int(sys.argv[1]) # h = int(sys.argv[2]) # except: # print( # "Error: use 'python maze.py int-width int-height' replacing int-width and int-height with a number for width and height of the maze") # main(w, h, random.randint(1, w - 1), random.randint(1, h - 1)) # Don't use random while testing main(10, 10, 3, 7)What was happening is that you originally made your maze by creating a list of elements, and then expanded that list as references to the first. This is an oddity of python that many (including myself) get fooled by initially. results: [/i]
RE: Change single element in 2D list changes every 1D element - AceScottie - Nov-13-2017 thank you. I sort of understand however when i was testing i was able to change an entire row: maze[5] = [1, 1, 1, 1, 1, 1, 1, 1, 1]originally i also created the list differently maze = [] mazer = [0] * w for i in range(0, h) maze.append(mazer)and that seemed to have the same problem. however printed out using the same method perfectly fine. RE: Change single element in 2D list changes every 1D element - Larz60+ - Nov-13-2017 Take a look at: https://docs.python.org/2/library/copy.html it explains it better than I can |