• Advertisement
Sign in to follow this  

Trying to modify a 2D list in Python

This topic is 1737 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can someone help me with a bug I'm having with modifying a 2D list? I'm making deep copies but the original is getting modified for some reason. I don't know how this is happening. Thanks.

 

https://gist.github.com/wokumura/5425191

OBJECTIMAGES = ['#', '$', '@', '.']
mapTiles =  [[None, '#', None, None, None, None, None, None, None, None], 
            [None, None, None, None, None, None, None, None, None, None], 
            [None, None, None, None, None, None, None, None, None, None], 
            [None, None, None, None, None, None, None, None, None, None], 
            [None, None, None, None, None, None, None, None, None, None], 
            [None, None, None, None, None, None, None, None, None, None], 
            [None, None, None, None, None, None, None, None, None, None], 
            [None, None, None, None, None, None, None, None, None, None], 
            [None, None, None, None, None, None, None, None, None, None], 
            [None, None, None, None, None, None, None, None, None, None]]
            
newMap = mapTiles[:]
for x in newMap[:]:
    if set(OBJECTIMAGES).intersection(x) == set([]):
        newMap.remove(x)
    else:
        break
tempMap = newMap[:]
objectFound = False
for row in range(len(newMap[0])):
    for x in range(len(newMap)):
        print x, row, mapTiles[0][1]
        if newMap[x][row] in OBJECTIMAGES:
            objectFound = True
            break
    if objectFound:
        break
    else:
        for i in range(len(newMap)):
            del tempMap[0]

 

Share this post


Link to post
Share on other sites
Advertisement
I think [:] is not enough, because it only copies the outer list. Try using deepcopy(). Or list comprehension. Edited by ultramailman

Share this post


Link to post
Share on other sites

Thank you. The deepcopy function worked. I wasn't aware that [:] only copied the outer list.

 

I think [:] is not enough, because it only copies the outer list. Try using deepcopy(). Or list comprehension.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement