Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > using "*" to make a list of lists with repeated (and independent) elements

Reply
Thread Tools

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

 
 
TP
Guest
Posts: n/a
 
      09-26-2012
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
 
Reply With Quote
 
 
 
 
Ian Kelly
Guest
Posts: n/a
 
      09-26-2012
On Wed, Sep 26, 2012 at 3:20 PM, TP <(E-Mail Removed)> wrote:
> 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.


Mostly correct. When you do [foo] * 3 it extends the list with the
*same objects* no matter what type they are. In the case of integers,
it doesn't matter that it's the same objects, because integers are
immutable. Lists are mutable, however, and so it becomes apparent
that the same objects are repeated when you try to modify one of the
lists.

> In these conditions, how to make this list [[0,0,0],[0,0,0]] with "*"
> without this behavior?


Use a list comprehension:

a = [[0] * 3 for _ in range(2)]

This way the expression `[0] * 3` is re-evaluated at each position in
the outer list, rather than evaluated just once and then copied.
 
Reply With Quote
 
 
 
 
Paul Rubin
Guest
Posts: n/a
 
      09-26-2012
TP <(E-Mail Removed)> 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]]
 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      09-26-2012
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.
 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      09-26-2012
Paul Rubin於 2012年9月27日星期四UTC+8上午5時43分58秒 寫道:
> TP <(E-Mail Removed)> 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.

 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      09-26-2012
88888 Dihedral於 2012年9月27日星期四UTC+8上午6時07分35秒 寫道:
> Paul Rubin於 2012年9月27日星期四UTC+8上午5時43分58秒 寫道:
>
> > TP <(E-Mail Removed)> 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.

 
Reply With Quote
 
Tim Chase
Guest
Posts: n/a
 
      09-26-2012
On 09/26/12 17:28, 88888 Dihedral wrote:
> 88888 Dihedral於 2012年9月27日星期四UTC+8上午6時07分35秒 寫道:
>>>> 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]]

>
> 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.


But this is Python, so why the heck would anybody want to emulate
*C* style? It could also be written in an assembly-language style,
COBOL style, or a Fortran style...none of which are particularly
valuable.

Besides, a C-style would allocate a single array of M*N slots and
then calculate 2d offsets into that single array.

-tkc



 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      09-26-2012
Tim Chase於 2012年9月27日星期四UTC+8上午6時44分42秒 寫道:
> On 09/26/12 17:28, 88888 Dihedral wrote:
>
> > 88888 Dihedral於 2012年9月27日星期四UTC+8上午6時07分35秒 寫道:

>
> >>>> 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]]

>
> >

>
> > 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.

>
>
>
> But this is Python, so why the heck would anybody want to emulate
>
> *C* style? It could also be written in an assembly-language style,
>
> COBOL style, or a Fortran style...none of which are particularly
>
> valuable.
>
>
>
> Besides, a C-style would allocate a single array of M*N slots and
>
> then calculate 2d offsets into that single array.
>
>
>
> -tkc


I don't think a lot programmers can write assembly programs well
for different instruction sets of cpus.

Of course if GCC was not supportd in manny platforms free
of charge, then I won't recommend this style of
programming in python.
 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      09-26-2012
Tim Chase於 2012年9月27日星期四UTC+8上午6時44分42秒 寫道:
> On 09/26/12 17:28, 88888 Dihedral wrote:
>
> > 88888 Dihedral於 2012年9月27日星期四UTC+8上午6時07分35秒 寫道:

>
> >>>> 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]]

>
> >

>
> > 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.

>
>
>
> But this is Python, so why the heck would anybody want to emulate
>
> *C* style? It could also be written in an assembly-language style,
>
> COBOL style, or a Fortran style...none of which are particularly
>
> valuable.
>
>
>
> Besides, a C-style would allocate a single array of M*N slots and
>
> then calculate 2d offsets into that single array.
>
>
>
> -tkc


I don't think a lot programmers can write assembly programs well
for different instruction sets of cpus.

Of course if GCC was not supportd in manny platforms free
of charge, then I won't recommend this style of
programming in python.
 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      09-27-2012
Tim Chase於 2012年9月27日星期四UTC+8上午6時44分42秒 寫道:
> On 09/26/12 17:28, 88888 Dihedral wrote:
>
> > 88888 Dihedral於 2012年9月27日星期四UTC+8上午6時07分35秒 寫道:

>
> >>>> 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]]

>
> >

>
> > 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.

>
>
>
> But this is Python, so why the heck would anybody want to emulate
>
> *C* style? It could also be written in an assembly-language style,
>
> COBOL style, or a Fortran style...none of which are particularly
>
> valuable.
>
>
>
> Besides, a C-style would allocate a single array of M*N slots and
>
> then calculate 2d offsets into that single array.
>
>
>
> -tkc


a=[1, 2,3]
b=[a]*4
print b
a[1]=4
print b

I thnik this is very clear about the situation in entangled references.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
concatenate the elements in each list of a list of lists antar2 Python 3 07-23-2008 08:21 PM
common elements between list of lists and lists antar2 Python 2 07-17-2008 09:19 AM
Appending a list's elements to another list using a list comprehension Debajit Adhikary Python 17 10-18-2007 06:45 PM
List of lists of lists of lists... =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==?= Python 5 05-15-2006 11:47 AM
Removing elements from a list that are elements in another list Adam Hartshorne C++ 2 01-27-2006 07:47 AM



Advertisments