why this happend using model random?

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


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?



More information about the Python-list mailing list