Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
using "*" to make a list of lists with repeated (and independent) elements
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  19 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
TP  
View profile  
 More options Sep 26 2012, 5:25 pm
Newsgroups: comp.lang.python
From: TP <T...@frenoespam.fr.invalid>
Date: Wed, 26 Sep 2012 23:20:10 +0200
Local: Wed, Sep 26 2012 5:20 pm
Subject: using "*" to make a list of lists with repeated (and independent) elements
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ian Kelly  
View profile  
 More options Sep 26 2012, 5:40 pm
Newsgroups: comp.lang.python
From: Ian Kelly <ian.g.ke...@gmail.com>
Date: Wed, 26 Sep 2012 15:39:32 -0600
Local: Wed, Sep 26 2012 5:39 pm
Subject: Re: using "*" to make a list of lists with repeated (and independent) elements

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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Paul Rubin  
View profile  
 More options Sep 26 2012, 5:43 pm
Newsgroups: comp.lang.python
From: Paul Rubin <no.em...@nospam.invalid>
Date: Wed, 26 Sep 2012 14:43:58 -0700
Local: Wed, Sep 26 2012 5:43 pm
Subject: Re: using "*" to make a list of lists with repeated (and independent) elements

TP <T...@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]]

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
88888 Dihedral  
View profile  
 More options Sep 26 2012, 5:45 pm
Newsgroups: comp.lang.python
From: 88888 Dihedral <dihedral88...@googlemail.com>
Date: Wed, 26 Sep 2012 14:45:58 -0700 (PDT)
Local: Wed, Sep 26 2012 5:45 pm
Subject: Re: using "*" to make a list of lists with repeated (and independent) elements
TP於 2012年9月27日星期四UTC+8上午5時25分04秒寫道:

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.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
88888 Dihedral  
View profile  
 More options Sep 26 2012, 6:07 pm
Newsgroups: comp.lang.python
From: 88888 Dihedral <dihedral88...@googlemail.com>
Date: Wed, 26 Sep 2012 15:07:35 -0700 (PDT)
Local: Wed, Sep 26 2012 6:07 pm
Subject: Re: using "*" to make a list of lists with repeated (and independent) elements
Paul Rubin於 2012年9月27日星期四UTC+8上午5時43分58秒寫道:

I used numpy before.

Python is not lisp but python can emulate the lisp behaviors.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
88888 Dihedral  
View profile   Translate to Translated (View Original)
 More options Sep 26 2012, 6:28 pm
Newsgroups: comp.lang.python
From: 88888 Dihedral <dihedral88...@googlemail.com>
Date: Wed, 26 Sep 2012 15:28:15 -0700 (PDT)
Local: Wed, Sep 26 2012 6:28 pm
Subject: Re: using "*" to make a list of lists with repeated (and independent) elements
88888 Dihedral於 2012年9月27日星期四UTC+8上午6時07分35秒寫道:

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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tim Chase  
View profile  
 More options Sep 26 2012, 6:44 pm
Newsgroups: comp.lang.python
From: Tim Chase <python.l...@tim.thechases.com>
Date: Wed, 26 Sep 2012 17:45:19 -0500
Local: Wed, Sep 26 2012 6:45 pm
Subject: Re: using "*" to make a list of lists with repeated (and independent) elements
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. :-P

-tkc


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
88888 Dihedral  
View profile   Translate to Translated (View Original)
 More options Sep 26 2012, 6:53 pm
Newsgroups: comp.lang.python
From: 88888 Dihedral <dihedral88...@googlemail.com>
Date: Wed, 26 Sep 2012 15:53:40 -0700 (PDT)
Local: Wed, Sep 26 2012 6:53 pm
Subject: Re: using "*" to make a list of lists with repeated (and independent) elements
Tim Chase於 2012年9月27日星期四UTC+8上午6時44分42秒寫道:

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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
88888 Dihedral  
View profile   Translate to Translated (View Original)
 More options Sep 26 2012, 6:53 pm
Newsgroups: comp.lang.python
From: 88888 Dihedral <dihedral88...@googlemail.com>
Date: Wed, 26 Sep 2012 15:53:40 -0700 (PDT)
Local: Wed, Sep 26 2012 6:53 pm
Subject: Re: using "*" to make a list of lists with repeated (and independent) elements
Tim Chase於 2012年9月27日星期四UTC+8上午6時44分42秒寫道:

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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
88888 Dihedral  
View profile   Translate to Translated (View Original)
 More options Sep 27 2012, 5:24 pm
Newsgroups: comp.lang.python
From: 88888 Dihedral <dihedral88...@googlemail.com>
Date: Thu, 27 Sep 2012 14:24:00 -0700 (PDT)
Subject: Re: using "*" to make a list of lists with repeated (and independent) elements
Tim Chase於 2012年9月27日星期四UTC+8上午6時44分42秒寫道:

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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
88888 Dihedral  
View profile   Translate to Translated (View Original)
 More options Sep 27 2012, 5:24 pm
Newsgroups: comp.lang.python
From: 88888 Dihedral <dihedral88...@googlemail.com>
Date: Thu, 27 Sep 2012 14:24:00 -0700 (PDT)
Subject: Re: using "*" to make a list of lists with repeated (and independent) elements
Tim Chase於 2012年9月27日星期四UTC+8上午6時44分42秒寫道:

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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ramchandra Apte  
View profile  
 More options Sep 29 2012, 9:46 am
Newsgroups: comp.lang.python
From: Ramchandra Apte <maniandra...@gmail.com>
Date: Sat, 29 Sep 2012 06:46:22 -0700 (PDT)
Local: Sat, Sep 29 2012 9:46 am
Subject: Re: using "*" to make a list of lists with repeated (and independent) elements

88888 Dihedral is a bot.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ramchandra Apte  
View profile  
 More options Sep 29 2012, 9:46 am
Newsgroups: comp.lang.python
From: Ramchandra Apte <maniandra...@gmail.com>
Date: Sat, 29 Sep 2012 06:46:22 -0700 (PDT)
Local: Sat, Sep 29 2012 9:46 am
Subject: Re: using "*" to make a list of lists with repeated (and independent) elements

88888 Dihedral is a bot.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
88888 Dihedral  
View profile  
 More options Sep 29 2012, 1:01 pm
Newsgroups: comp.lang.python
From: 88888 Dihedral <dihedral88...@googlemail.com>
Date: Sat, 29 Sep 2012 10:01:30 -0700 (PDT)
Local: Sat, Sep 29 2012 1:01 pm
Subject: Re: using "*" to make a list of lists with repeated (and independent) elements

Don't you get it why I avoided the lambda one liner as a functon.

I prefer the def way with a name chosen.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
88888 Dihedral  
View profile  
 More options Sep 29 2012, 1:02 pm
Newsgroups: comp.lang.python
From: 88888 Dihedral <dihedral88...@googlemail.com>
Date: Sat, 29 Sep 2012 10:01:30 -0700 (PDT)
Local: Sat, Sep 29 2012 1:01 pm
Subject: Re: using "*" to make a list of lists with repeated (and independent) elements

Don't you get it why I avoided the lambda one liner as a functon.

I prefer the def way with a name chosen.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ian Kelly  
View profile  
 More options Sep 29 2012, 1:19 pm
Newsgroups: comp.lang.python
From: Ian Kelly <ian.g.ke...@gmail.com>
Date: Sat, 29 Sep 2012 11:18:48 -0600
Local: Sat, Sep 29 2012 1:18 pm
Subject: Re: using "*" to make a list of lists with repeated (and independent) elements
On Sat, Sep 29, 2012 at 11:01 AM, 88888 Dihedral

<dihedral88...@googlemail.com> wrote:

> Don't you get it why I avoided the lambda one liner as a functon.

> I prefer the def way with a name chosen.

Certainly, but the Bresenham line algorithm is O(n), which is why it
is so superior to quicksort that is O(n log n).  Of course you might
try the Capo Ferro optimization, but I find that Thibault cancels out
Capo Ferro, don't you?

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Angelico  
View profile  
 More options Sep 29 2012, 1:41 pm
Newsgroups: comp.lang.python
From: Chris Angelico <ros...@gmail.com>
Date: Sun, 30 Sep 2012 03:41:04 +1000
Local: Sat, Sep 29 2012 1:41 pm
Subject: Re: using "*" to make a list of lists with repeated (and independent) elements

On Sun, Sep 30, 2012 at 3:18 AM, Ian Kelly <ian.g.ke...@gmail.com> wrote:
> On Sat, Sep 29, 2012 at 11:01 AM, 88888 Dihedral
> <dihedral88...@googlemail.com> wrote:

>> Don't you get it why I avoided the lambda one liner as a functon.

>> I prefer the def way with a name chosen.

> Certainly, but the Bresenham line algorithm is O(n), which is why it
> is so superior to quicksort that is O(n log n).  Of course you might
> try the Capo Ferro optimization, but I find that Thibault cancels out
> Capo Ferro, don't you?

Unless ...

class Agrippa(object):
    students = []
    def __init__(self):
        students.append(self)

enemy = Agrippa()

ChrisA
(maybe I'm pushing this a bit too far)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
88888 Dihedral  
View profile  
 More options Sep 29 2012, 2:50 pm
Newsgroups: comp.lang.python
From: 88888 Dihedral <dihedral88...@googlemail.com>
Date: Sat, 29 Sep 2012 11:50:52 -0700 (PDT)
Local: Sat, Sep 29 2012 2:50 pm
Subject: Re: using "*" to make a list of lists with repeated (and independent) elements

OK! I'll illustrate the lazy aspect of the python interpreter furthermore.

a=[1,2,3]
b=[a]*4 # different from a sliced copy as [list(a)]*4
print b
a[1]=4
print b
#-----
a=666 # type morphed
print b


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
88888 Dihedral  
View profile  
 More options Sep 29 2012, 2:51 pm
Newsgroups: comp.lang.python
From: 88888 Dihedral <dihedral88...@googlemail.com>
Date: Sat, 29 Sep 2012 11:50:52 -0700 (PDT)
Local: Sat, Sep 29 2012 2:50 pm
Subject: Re: using "*" to make a list of lists with repeated (and independent) elements

OK! I'll illustrate the lazy aspect of the python interpreter furthermore.

a=[1,2,3]
b=[a]*4 # different from a sliced copy as [list(a)]*4
print b
a[1]=4
print b
#-----
a=666 # type morphed
print b


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »