using "*" to make a list of lists with repeated (and independent) elements

88888 Dihedral dihedral88888 at googlemail.com
Wed Sep 26 18:28:15 EDT 2012


88888 Dihedral於 2012年9月27日星期四UTC+8上午6時07分35秒寫道:
> Paul Rubin於 2012年9月27日星期四UTC+8上午5時43分58秒寫道:
> 
> > TP <TP at frenoespam.fr.invalid> writes:
> 
> > 
> 
> > > copies a list, he copies in fact the *pointer* to the list ....
> 
> > 
> 
> > > Is it the correct explanation?
> 
> > 
> 
> > 
> 
> > 
> 
> > Yes, that is correct.
> 
> > 
> 
> > 
> 
> > 
> 
> > > In these conditions, how to make this list [[0,0,0],[0,0,0]] with "*" 
> 
> > 
> 
> > > without this behavior?
> 
> > 
> 
> > 
> 
> > 
> 
> >     >>> a = [[0]*3 for i in xrange(2)]
> 
> > 
> 
> >     >>> a[0][0]=2
> 
> > 
> 
> >     >>> a
> 
> > 
> 
> >     [[2, 0, 0], [0, 0, 0]]
> 
> 
> 
> I used numpy before. 
> 
> 
> 
> Python is not lisp but python can emulate the lisp behaviors.

def zeros(m,n):
	a=[]
	for i in xrange(m):
		a.append([0]*n)
	return a

If  one wants to tranlate to C, this is the style.




More information about the Python-list mailing list