Jump to content

View more

Image of the Day

The night is still, but the invasion brings chaos. #screenshotsaturday #hanako #indiegame #gameart #ue4 #samurai https://t.co/cgILXuokoS
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

Trying to modify a 2D list in Python

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
2 replies to this topic

#1 tp9   Members   

537
Like
0Likes
Like

Posted 20 April 2013 - 02:13 AM

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[i][0]

 



#2 ultramailman   Prime Members   

1720
Like
2Likes
Like

Posted 20 April 2013 - 02:30 AM

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

Edited by ultramailman, 20 April 2013 - 02:30 AM.


#3 tp9   Members   

537
Like
0Likes
Like

Posted 20 April 2013 - 02:45 AM

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.






Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.