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
#2
What do you want ?
a. some pixel should be 1
b. all pixel should be 1
Reply
#3
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))
Reply
#4
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
Reply
#5
' something like ' d0esn't work in programming,
what was the actual input?
Reply
#6
(Nov-13-2017, 05:43 PM)Larz60+ Wrote: ' something like ' d0esn't work in programming,
what was the actual input?

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
Reply
#7
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]
Reply
#8
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:
Output:
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, 0, 0, 0] [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [0, 0, 0, 0, 0, 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]
[/i]
Reply
#9
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.
Reply
#10
Take a look at: https://docs.python.org/2/library/copy.html
it explains it better than I can
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Fixture not returning webdriver element Nik1811 1 134 Yesterday, 04:39 PM
Last Post: Nik1811
  element in list detection problem jacksfrustration 5 302 Apr-11-2024, 05:44 PM
Last Post: deanhystad
  Elegant way to apply each element of an array to a dataframe? sawtooth500 7 369 Mar-29-2024, 05:51 PM
Last Post: deanhystad
  If a set element has digits in the element tester_V 3 268 Mar-25-2024, 04:43 PM
Last Post: deanhystad
  Variable for the value element in the index function?? Learner1 8 622 Jan-20-2024, 09:20 PM
Last Post: Learner1
  Searche each element of each tuple based 3 numbes zinho 8 852 Dec-11-2023, 05:14 PM
Last Post: zinho
  list in dicitonary element problem jacksfrustration 3 687 Oct-14-2023, 03:37 PM
Last Post: deanhystad
  Change font in a list or tuple apffal 4 2,666 Jun-16-2023, 02:55 AM
Last Post: schriftartenio
  search an element in pandas series learningPython 5 1,397 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 799 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