0

Okay. So I'm in Programing fundamentals and for our final project we have to design a game with a map. I created a program that creates a list of ten lists with ten items in each. However when I try to change a single item in one list (for example mapGrid[4][5] = "i") it changes all of the 6th items in all of the lists. Here is the code I'm using to test it:

import random

def createMap():

    mapGrid = []
    col = []
    randx = 0
    randy = 0
    i = 0

    for x in range(0,10):
        col += "#"

    for y in range(0,10):
        mapGrid.append(col)

    while i < 10:
        randx = random.randint(0,9)
        randy = random.randint(0,9)
        if mapGrid[randx][randy] != "i":
            mapGrid[randx][randy] = "i"
            i += 1

    return mapGrid

def printMap(x,y,mapGrid):

    mapGrid[x][y] = "0"

    print("",mapGrid[x-1][y+1],mapGrid[x][y+1],mapGrid[x+1][y+1],"\n",
          mapGrid[x-1][y],mapGrid[x][y],mapGrid[x+1][y],"\n",
          mapGrid[x-1][y-1],mapGrid[x][y-1],mapGrid[x+1][y-1])

examp = createMap()
print(examp)
print("")
printMap(4,4,examp)

and the result I keep getting is:

[['i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i'], ['i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i'], ['i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i'], ['i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i'], ['i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i'], ['i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i'], ['i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i'], ['i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i'], ['i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i'], ['i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i']]

 i i i
 0 0 0 
 i i i

instead of there being ten single i's and one '0' it becomes all i's and every 5th item in each list is a 0.

How do I fix it so I only change a single item instead of an item in every list?

3 Answers 3

1

You're appending the same col list several times, what you want is to create a new copy each time:

mapGrid.append(col[:]) # a copy of col

See live example

Sign up to request clarification or add additional context in comments.

Comments

0

You are adding 10 times the identical list col to your map grid, it's not a copy each time. So every change in your while loop affects every column/sublist in your mapGrid in the same way - not just one of them.

Changing

for y in range(0,10):
        mapGrid.append(col)

To

for y in range(0,10):
        mapGrid.append(list(col))

Outputs

[['i', '#', '#', '#', '#', '#', '#', '#', '#', '#'], ['#', '#', '#', '#', 'i', '
#', 'i', '#', '#', '#'], ['#', '#', '#', '#', '#', '#', '#', '#', '#', '#'], ['#
', '#', '#', '#', '#', '#', '#', '#', '#', '#'], ['#', '#', '#', '#', '#', '#',
'i', '#', '#', '#'], ['#', '#', '#', '#', 'i', '#', '#', 'i', '#', '#'], ['#', '
#', '#', '#', '#', 'i', '#', '#', '#', '#'], ['#', '#', 'i', '#', '#', '#', '#',
 '#', '#', '#'], ['#', 'i', '#', '#', '#', '#', '#', '#', '#', '#'], ['#', '#',
'#', '#', '#', 'i', '#', '#', '#', '#']]

('', '#', '#', '#', '\n', '#', '0', 'i', '\n', '#', '#', '#')

Comments

0

The problem is you've created a list with ten references to the same list, in this:

for y in range(0,10):
    mapGrid.append(col)

You need to construct a new list each time - you can do this using col[:], as per this FAQ entry, or by constructing a new one explicitly with list(col).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.