Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #30239

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

Newsgroups comp.lang.python
Date 2012-09-26 14:45 -0700
References <b58cj9-h5d.ln1@rama.fbx.proxad.net>
Message-ID <a194f613-807e-4c2a-808e-3087df6111f4@googlegroups.com> (permalink)
Subject Re: using "*" to make a list of lists with repeated (and independent) elements
From 88888 Dihedral <dihedral88888@googlemail.com>

Show all headers | View raw


TP於 2012年9月27日星期四UTC+8上午5時25分04秒寫道:
> Hi everybody,
> 
> 
> 
> I have tried, naively, to do the following, so as to make lists quickly:
> 
> 
> 
> >>> a=[0]*2
> 
> >>> a
> 
> [0, 0]
> 
> >>> a[0]=3
> 
> >>> a
> 
> [3, 0]
> 
> 
> 
> All is working fine, so I extended the technique to do:
> 
> 
> 
> >>> a=[[0]*3]*2
> 
> >>> a
> 
> [[0, 0, 0], [0, 0, 0]]
> 
> >>> a[0][0]=2
> 
> >>> a
> 
> [[2, 0, 0], [2, 0, 0]]
> 
> 
> 
> The behavior is no more expected!
> 
> The reason is probably that in the first case, 0 is an integer, not a list, 
> 
> so Python copies two elements that are independent.
> 
> In the second case, the elements are [0,0,0], which is a list; when Python 
> 
> copies a list, he copies in fact the *pointer* to the list, such that we 
> 
> obtain this apparently strange behavior.
> 
> 
> 
> Is it the correct explanation?
> 
> In these conditions, how to make this list [[0,0,0],[0,0,0]] with "*" 
> 
> without this behavior?
> 
> 
> 
> Thanks,
> 
> 
> 
> TP

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

>>> a=zeros(3,2)
>>> a
[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
>>> 

I think this is what you want.

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

using "*" to make a list of lists with repeated (and independent) elements TP <TP@frenoespam.fr.invalid> - 2012-09-26 23:20 +0200
  Re: using "*" to make a list of lists with repeated (and independent) elements Ian Kelly <ian.g.kelly@gmail.com> - 2012-09-26 15:39 -0600
  Re: using "*" to make a list of lists with repeated (and independent) elements Paul Rubin <no.email@nospam.invalid> - 2012-09-26 14:43 -0700
    Re: using "*" to make a list of lists with repeated (and independent) elements 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-26 15:07 -0700
      Re: using "*" to make a list of lists with repeated (and independent) elements 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-26 15:28 -0700
        Re: using "*" to make a list of lists with repeated (and independent) elements Tim Chase <python.list@tim.thechases.com> - 2012-09-26 17:45 -0500
          Re: using "*" to make a list of lists with repeated (and independent) elements 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-26 15:53 -0700
          Re: using "*" to make a list of lists with repeated (and independent) elements 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-26 15:53 -0700
          Re: using "*" to make a list of lists with repeated (and independent) elements 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-27 14:24 -0700
          Re: using "*" to make a list of lists with repeated (and independent) elements 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-27 14:24 -0700
          Re: using "*" to make a list of lists with repeated (and independent) elements Ramchandra Apte <maniandram01@gmail.com> - 2012-09-29 06:46 -0700
            Re: using "*" to make a list of lists with repeated (and independent) elements 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-29 10:01 -0700
              Re: using "*" to make a list of lists with repeated (and independent) elements Ian Kelly <ian.g.kelly@gmail.com> - 2012-09-29 11:18 -0600
                Re: using "*" to make a list of lists with repeated (and independent) elements 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-29 11:50 -0700
                Re: using "*" to make a list of lists with repeated (and independent) elements 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-29 11:50 -0700
              Re: using "*" to make a list of lists with repeated (and independent) elements Chris Angelico <rosuav@gmail.com> - 2012-09-30 03:41 +1000
            Re: using "*" to make a list of lists with repeated (and independent) elements 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-29 10:01 -0700
          Re: using "*" to make a list of lists with repeated (and independent) elements Ramchandra Apte <maniandram01@gmail.com> - 2012-09-29 06:46 -0700
  Re: using "*" to make a list of lists with repeated (and independent) elements 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-26 14:45 -0700

csiph-web