Python Forum
Change single element in 2D list changes every 1D element
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Change single element in 2D list changes every 1D element
#1
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))
Reply


Messages In This Thread
Change single element in 2D list changes every 1D element - by AceScottie - Nov-12-2017, 08:10 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Fixture not returning webdriver element Nik1811 1 350 Apr-15-2024, 04:39 PM
Last Post: Nik1811
  element in list detection problem jacksfrustration 5 630 Apr-11-2024, 05:44 PM
Last Post: deanhystad
  Elegant way to apply each element of an array to a dataframe? sawtooth500 7 614 Mar-29-2024, 05:51 PM
Last Post: deanhystad
  If a set element has digits in the element tester_V 3 438 Mar-25-2024, 04:43 PM
Last Post: deanhystad
  Variable for the value element in the index function?? Learner1 8 819 Jan-20-2024, 09:20 PM
Last Post: Learner1
  Searche each element of each tuple based 3 numbes zinho 8 1,037 Dec-11-2023, 05:14 PM
Last Post: zinho
  list in dicitonary element problem jacksfrustration 3 847 Oct-14-2023, 03:37 PM
Last Post: deanhystad
  Change font in a list or tuple apffal 4 2,811 Jun-16-2023, 02:55 AM
Last Post: schriftartenio
  search an element in pandas series learningPython 5 1,552 Apr-30-2023, 08:34 AM
Last Post: learningPython
Question Inserting Numerical Value to the Element in Optionlist and Printing it into Entry drbilgehanbakirhan 1 899 Jan-30-2023, 05:16 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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