Re: [Tutor] List of lists help
"Mark Tolonen" <[EMAIL PROTECTED]> wrote L = [[0,0,0] *3] I think you meant: [[0,0,0]]*3 [[0, 0, 0], [0, 0, 0], [0, 0, 0]] Yes indeed, to much haste and not enough testing! Alan G ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] List of lists help
"Alan Gauld" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] [snip] Something like L = [[0,0,0] *3] I think you meant: [[0,0,0]]*3 [[0, 0, 0], [0, 0, 0], [0, 0, 0]] -Mark Now L contains 3 copies of the same list so when you change any one copy it is reflected in all of the other copies. L[0][1] = 6 L [[0,6,0],[0,6,0],[0,6,0]] ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] List of lists help
<[EMAIL PROTECTED]> wrote self.boardarray[row][col] = self.currentPlayer 4, self.boardarray[2][4] will be set to "2". Instead, the program is setting multiple values within the list as 2. Here is an example output, when I click on (0,0): [[2, 0, 0, 0, 0], [2, 0, 0, 0, 0], [2, 0, 0, 0, 0], etc. This is almost always caused by you initialising the data wrongly. You have set every item in your list to point to the same sublist. Something like L = [[0,0,0] *3] Now L contains 3 copies of the same list so when you change any one copy it is reflected in all of the other copies. L[0][1] = 6 L [[0,6,0],[0,6,0],[0,6,0]] You can avoid this by constricting the list using a list comprehension (or any of several other solutions) L = [[0,0,0] for n in range(3)] L[0][1] = 7 L [[0, 7, 0], [0, 0, 0], [0, 0, 0]] Remember that Python does everything by references so a = foo b = foo leaves a and b pointing at the same foo object not two copies of foo. The same applies to lists. If you want a copy you need to explicitly make a copy. L1 = [1,2] L2 = L1[:] Now L1 and L2 are two separate lists. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] List of lists help
[EMAIL PROTECTED] wrote: Hello, I am completely baffled by this action and would appreciate any help. My problem is occurring within a class which is within a larger program; if I need to post the entire program let me know and I will. It's only about 250 lines so far. Anyways, I am using a list of lists to store data in a GUI game called goMoku. It is kind of like a connect five game, pretty simple. When a user clicks a certain square, this line is called to store the "move": self.boardarray[row][col] = self.currentPlayer The problem is most likely in how you construct your data structure. If you want to post a follow-up with code, extract those lines and see if you can construct the problem in <= 10 lines of code. (any code with more than 15 lines is most likely too large to understand in the time that people take to read a post here) With respect to your problem, a similar problem has been discussed recently at this list with dictionaries instead of lists, please read http://mail.python.org/pipermail/tutor/2008-November/065283.html If it doesn't answer your question, try to re-construct the problem in a small example, and post that. Sincerely, Albert ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor