why this happend using model random?

flyaflya flyaflya at gmail.com
Tue May 10 10:59:07 EDT 2005


flyaflya wrote:
> Robert Kern wrote:
> 
>> flyaflya wrote:
>>
>>> from random import *
>>>
>>> col = [0 for i in range(10)]
>>> a = [col for  i in range(10)]
>>
>>
>>
>> This is the problem. The list "a" now has ten references to the same 
>> object "col". They are not copied.
>>
>>> seed()
>>> for i  in range(10):
>>>      for j in  range(10):
>>>          a[i][j] = randint(0, 100)
>>
>>
>>
>> So every time you index into "a[i][j]" you are always getting "col[j]".
>>
> thanks,I see,I know about reference and copy,but when is it a copy?when 
> a reference?how can I get a copy when need?
I find the answer on <cook book> chapter4.5,it's a practical book for 
the newbies like me,maybe many newbie using c/c++ would make such mistake



More information about the Python-list mailing list