Re: Changing a Value in List of lists

2012-09-02 Thread Rishabh Dixit
On Sunday, 2 September 2012 23:40:32 UTC+5:30, Terry Reedy wrote: > On 9/2/2012 7:17 AM, Mark Lawrence wrote: > > > On 02/09/2012 12:05, Rishabh Dixit wrote: > > >> Hi, > > >> > > >> I got it :). We should create a new list every time before adding to a > > >> list > > >> of list so that ins

Re: Changing a Value in List of lists

2012-09-02 Thread Terry Reedy
On 9/2/2012 7:17 AM, Mark Lawrence wrote: On 02/09/2012 12:05, Rishabh Dixit wrote: Hi, I got it :). We should create a new list every time before adding to a list of list so that instead repeating the reference a new reference generated every time. Please don't top post on this list as it o

Re: Changing a Value in List of lists

2012-09-02 Thread Mark Lawrence
On 02/09/2012 12:05, Rishabh Dixit wrote: Hi, I got it :). We should create a new list every time before adding to a list of list so that instead repeating the reference a new reference generated every time. Please don't top post on this list as it often destroys the context, thanks. -- Chee

Re: Changing a Value in List of lists

2012-09-02 Thread Rishabh Dixit
Hi, I got it :). We should create a new list every time before adding to a list of list so that instead repeating the reference a new reference generated every time. >>> mat=[[0]*5 for i in range(5)] >>> mat[1][2]+=1 >>> mat [[0, 0, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0,

Re: Changing a Value in List of lists

2012-09-02 Thread Rishabh Dixit
Hi Chris, Thanks for your reply. I tried this - >>> ls=[0 for i in range(5)] >>> mat=[ls for i in range(5)] >>> mat[1][2]+=1 >>> mat [[0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0]] I guess here also the same issue is there as ls refers to one list that is be

Re: Changing a Value in List of lists

2012-09-02 Thread Chris Angelico
On Sun, Sep 2, 2012 at 7:44 PM, Rishabh Dixit wrote: > > Hi all, > > I have a doubt regarding how the list work in following case- > ls=[[0]*5]*5 ls[1][1]+=1 ls > [[0, 1, 0, 0, 0], [0, 1, 0, 0, 0], [0, 1, 0, 0, 0], [0, 1, 0, 0, 0], [0, 1, > 0, 0, 0]] > > > Here, according to me only

Changing a Value in List of lists

2012-09-02 Thread Rishabh Dixit
Hi all, I have a doubt regarding how the list work in following case- >>> ls=[[0]*5]*5 >>> ls[1][1]+=1 >>> ls [[0, 1, 0, 0, 0], [0, 1, 0, 0, 0], [0, 1, 0, 0, 0], [0, 1, 0, 0, 0], [0, 1, 0, 0, 0]] Here, according to me only one value in ls should be incremented but it is increasing 1 value in al