Jump to content

  • Log In with Google      Sign In   
  • Create Account

Trying to modify a 2D list in Python


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   -  Reputation: 533

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]

 



Sponsor:

#2 ultramailman   Prime Members   -  Reputation: 1585

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   -  Reputation: 533

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.



PARTNERS