List Problem

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2007
Posts: 37
Reputation: Azurea is an unknown quantity at this point 
Solved Threads: 3
Azurea's Avatar
Azurea Azurea is offline Offline
Light Poster

List Problem

 
0
  #1
May 14th, 2008
I've got an issue with lists right now. I've got 2 lists, Checker1 and Checker2, which check for the Ko rule in my game of Go. However, Checker2 should always be checking the board one turn behind Checker1, which checks the board every turn. That way, the Ko rule can be caught and dealt with. To do that, I have a Temp list which gets the contents of Checker1 before each run-through, and set it's contents to Checker2. Then the piece is added to the board. Checker1 gets the contents of the board, and Checker1 and Checker2 are compared to check for the Ko rule.

However, my problem is that for some reason, whenever Checker1 gets the contents of the board, Checker2's contents change to Checker1's contents (I've tested it through printing the 2 lists). Is there any particular reason why? I made copies of all of the lists doing

  1. list1 = list2[:]

But the lists are still having their contents shared. Any idea why this is?
Last edited by Azurea; May 14th, 2008 at 6:38 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 138
Reputation: a1eio is an unknown quantity at this point 
Solved Threads: 21
a1eio's Avatar
a1eio a1eio is offline Offline
Junior Poster

Re: List Problem

 
0
  #2
May 15th, 2008
there must be something else going on. When you make a copy of a list list2 = list1[:] it fills list2 with list1's values, but when you change either list1 or list2, the change isn't mirrored so list1 would stay the same if list2 was changed.

Are you sure your not doing something else incorrect? If you can, post the section of your code were the checker lists are used and changed etc then someone might be able to see if anything else is happening
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,056
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 298
woooee woooee is offline Offline
Veteran Poster

Re: List Problem

 
0
  #3
May 15th, 2008
If you are not changing the contents of one or both of the lists, then use a tuple instead:
  1. tuple1 = tuple(list2)
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,113
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 944
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: List Problem

 
0
  #4
May 15th, 2008
If your lists have other lists nested in them, then you need to use module copy and make a deepcopy.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 37
Reputation: Azurea is an unknown quantity at this point 
Solved Threads: 3
Azurea's Avatar
Azurea Azurea is offline Offline
Light Poster

Re: List Problem

 
0
  #5
May 15th, 2008
Ah, my Python teacher helped me out. Since I was returning the board list through a property in class, and I didn't return it using [:], it kept changing in the middle.

Thanks anyway though!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC