why this happend using model random?

F

flyaflya

from random import *

col = [0 for i in range(10)]
a = [col for i in range(10)]

seed()
for i in range(10):
for j in range(10):
a[j] = randint(0, 100)
print a

the result is:
[[78, 65, 35, 5, 68, 60, 1, 51, 81, 70],
[78, 65, 35, 5, 68, 60, 1, 51, 81, 70],
[78, 65, 35, 5, 68, 60, 1, 51, 81, 70], .....]
why result isn't true random?
but when the code like this:
from random import *
seed()
for i in range(10):
for j in range(10):
print randint(0, 100)
the result is true random, what's deffient between these two segment codes?
 
F

Fredrik Lundh

flyaflya said:
from random import *

col = [0 for i in range(10)]
a = [col for i in range(10)]
http://www.python.org/doc/faq/programming.html#how-do-i-create-a-multidimensional-list

seed()
for i in range(10):
for j in range(10):
a[j] = randint(0, 100)
print a

the result is:
[[78, 65, 35, 5, 68, 60, 1, 51, 81, 70],
[78, 65, 35, 5, 68, 60, 1, 51, 81, 70],
[78, 65, 35, 5, 68, 60, 1, 51, 81, 70], .....]
why result isn't true random?


because you've assigned all the values to the same list ("col"). see the link above
for details.

</F>
 
R

Robert Kern

flyaflya said:
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[j] = randint(0, 100)


So every time you index into "a[j]" you are always getting "col[j]".

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 
F

flyaflya

Robert said:
flyaflya said:
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[j] = randint(0, 100)



So every time you index into "a[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?
 
R

Robert Kern

flyaflya said:
Robert said:
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[j] = randint(0, 100)



So every time you index into "a[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?


Usually, references get passed around wherever possible. Just putting
"col" into the list comprehension like you did will never copy.

If you need a copy of a list, you could do "col[:]" or "list(col)". More
generally, see the "copy" module.

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 
F

flyaflya

flyaflya said:
Robert said:
flyaflya said:
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[j] = randint(0, 100)




So every time you index into "a[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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top