[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

brain stuck. whats occurring here?

Matthew_WARREN

2/7/2008 5:38:00 PM

Hallo,

I'm after

[[[],[],[],[],[]],[[],[],[],[],[]],[[],[],[],[],[]],[[],[],[],[],[]],[[],[],[],[],[]]]

(NxN 'grid', 5x5 in that example, and while typing this up i figured out
how to get it, but I'm still not sure what _was_ happening)


I'm trying

>>>> a=[]
>>> row=[ [] for n in range(0,10) ]
>>> a.extend(row[:])
>>> a
[[], [], [], [], [], [], [], [], [], []]
>>> a[0].extend(row[:])
>>> a
[[[...], [], [], [], [], [], [], [], [], []], [], [], [], [], [], [], [],
[], []]


why isnt that last a

[[[...]],[],[],[],[],[],[],[],[],[]]


Puzzled :)

Matt.
--


This message and any attachments (the "message") is
intended solely for the addressees and is confidential.
If you receive this message in error, please delete it and
immediately notify the sender. Any use not in accord with
its purpose, any dissemination or disclosure, either whole
or partial, is prohibited except formal approval. The internet
can not guarantee the integrity of this message.
BNP PARIBAS (and its subsidiaries) shall (will) not
therefore be liable for the message if modified.
Do not print this message unless it is necessary,
consider the environment.

---------------------------------------------

Ce message et toutes les pieces jointes (ci-apres le
"message") sont etablis a l'intention exclusive de ses
destinataires et sont confidentiels. Si vous recevez ce
message par erreur, merci de le detruire et d'en avertir
immediatement l'expediteur. Toute utilisation de ce
message non conforme a sa destination, toute diffusion
ou toute publication, totale ou partielle, est interdite, sauf
autorisation expresse. L'internet ne permettant pas
d'assurer l'integrite de ce message, BNP PARIBAS (et ses
filiales) decline(nt) toute responsabilite au titre de ce
message, dans l'hypothese ou il aurait ete modifie.
N'imprimez ce message que si necessaire,
pensez a l'environnement.
10 Answers

Mensanator

2/7/2008 6:16:00 PM

0

On Feb 7, 11:38 am, Matthew_WAR...@bnpparibas.com wrote:
> Hallo,
>
> I'm after
>
> [[[],[],[],[],[]],[[],[],[],[],[]],[[],[],[],[],[]],[[],[],[],[],[]],[[],[]­,[],[],[]]]
>
> (NxN 'grid', 5x5 in that example, and while typing this up i figured out
> how to get it, but I'm still not sure what _was_ happening)
>
> I'm trying
>
> >>>> a=[]
> >>> row=[ [] for n in range(0,10) ]
> >>> a.extend(row[:])
> >>> a
>
> [[], [], [], [], [], [], [], [], [], []]>>> a[0].extend(row[:])
> >>> a
>
> [[[...], [], [], [], [], [], [], [], [], []], [], [], [], [], [], [], [],
> [], []]
>
> why isnt that last a
>
> [[[...]],[],[],[],[],[],[],[],[],[]]
>
> Puzzled :)

I don't see why you should get either.

Especially considering this behaviour:

>>> a=[]
>>> row=[ [] for n in range(0,10) ]
>>> a.extend(row[:])
>>> a
[[], [], [], [], [], [], [], [], [], []]
>>> a[0].extend(row[:])
>>> a
[[[...], [], [], [], [], [], [], [], [], []], [], [], [], [], [], [],
[], [], []]

>>> a[0][0][0][0][0][0][0][0][0][0][0][0][0][0]
[[...], [], [], [], [], [], [], [], [], []]

>>> a=[]
>>> row=[ [] for n in range(0,10) ]
>>> a.extend(row[:])
>>> a
[[], [], [], [], [], [], [], [], [], []]

Bug in IDLE?

>
> Matt.
> --
>
> This message and any attachments (the "message") is
> intended solely for the addressees and is confidential.
> If you receive this message in error, please delete it and
> immediately notify the sender. Any use not in accord with
> its purpose, any dissemination or disclosure, either whole
> or partial, is prohibited except formal approval. The internet
> can not guarantee the integrity of this message.
> BNP PARIBAS (and its subsidiaries) shall (will) not
> therefore be liable for the message if modified.
> Do not print this message unless it is necessary,
> consider the environment.
>
>                 ---------------------------------------------
>
> Ce message et toutes les pieces jointes (ci-apres le
> "message") sont etablis a l'intention exclusive de ses
> destinataires et sont confidentiels. Si vous recevez ce
> message par erreur, merci de le detruire et d'en avertir
> immediatement l'expediteur. Toute utilisation de ce
> message non conforme a sa destination, toute diffusion
> ou toute publication, totale ou partielle, est interdite, sauf
> autorisation expresse. L'internet ne permettant pas
> d'assurer l'integrite de ce message, BNP PARIBAS (et ses
> filiales) decline(nt) toute responsabilite au titre de ce
> message, dans l'hypothese ou il aurait ete modifie.
> N'imprimez ce message que si necessaire,
> pensez a l'environnement.

Gabriel Genellina

2/7/2008 9:14:00 PM

0

En Thu, 07 Feb 2008 16:16:11 -0200, mensanator@aol.com
<mensanator@aol.com> escribió:
> On Feb 7, 11:38 am, Matthew_WAR...@bnpparibas.com wrote:

> I don't see why you should get either.
>
> Especially considering this behaviour:
>
>>>> a=[]
>>>> row=[ [] for n in range(0,10) ]
>>>> a.extend(row[:])
>>>> a
> [[], [], [], [], [], [], [], [], [], []]
>>>> a[0].extend(row[:])
>>>> a
> [[[...], [], [], [], [], [], [], [], [], []], [], [], [], [], [], [],
> [], [], []]

Those [...] should give a clue. Usually Python doesn't "shorten" a list
representation: if it takes a thousand lines to output a list, there will
be a thousand lines of output. The [...] means that the list is
*recursive*: it has an element that refers to the list itself, so it can't
be represented in the normal way.
Why is it recursive? row[:] is a new list, not the same object as row. It
is a copy - but a "shallow" copy, because their elements aren't copies
themselves. So row[0] is the same object as row[:][0]

>>> x = row[:]
>>> x == row
True
>>> x is row
False
>>> x[0] is row[0]
True

In the line a[0].extend(row[:]), a[0] is THE SAME LIST as row[:][0], the
first item you are appending. That is, the first thing extend() does is
conceptually a[0].append(a[0]) - and you got a recursive structure.

> Bug in IDLE?

No, just a misunderstanding of what [:] does, I presume.

--
Gabriel Genellina

Mensanator

2/8/2008 12:01:00 AM

0

On Feb 7, 3:13 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar> wrote:
> En Thu, 07 Feb 2008 16:16:11 -0200, mensana...@aol.com  
> <mensana...@aol.com> escribió:
>
> > On Feb 7, 11:38 am, Matthew_WAR...@bnpparibas.com wrote:
> > I don't see why you should get either.
>
> > Especially considering this behaviour:
>
> >>>> a=[]
> >>>> row=[ [] for n in range(0,10) ]
> >>>> a.extend(row[:])
> >>>> a
> > [[], [], [], [], [], [], [], [], [], []]
> >>>> a[0].extend(row[:])
> >>>> a
> > [[[...], [], [], [], [], [], [], [], [], []], [], [], [], [], [], [],
> > [], [], []]
>
> Those [...] should give a clue. Usually Python doesn't "shorten" a list  
> representation: if it takes a thousand lines to output a list, there will  
> be a thousand lines of output. The [...] means that the list is  
> *recursive*: it has an element that refers to the list itself, so it can't  
> be represented in the normal way.
> Why is it recursive? row[:] is a new list, not the same object as row. It  
> is a copy - but a "shallow" copy, because their elements aren't copies  
> themselves. So row[0] is the same object as row[:][0]
>
> >>> x = row[:]
> >>> x == row
> True
> >>> x is row
> False
> >>> x[0] is row[0]
>
> True
>
> In the line a[0].extend(row[:]), a[0] is THE SAME LIST as row[:][0], the  
> first item you are appending. That is, the first thing extend() does is  
> conceptually a[0].append(a[0]) - and you got a recursive structure.

I thought that it was some kind of recursive hocus pocus.
That explains why no matter how many indexes I ask for,
I always get the same result.

>>> a[0][0][0][0][0][0][0][0][0][0][0][0][0][0]
[[...], [], [], [], [], [], [], [], [], []]

>
> > Bug in IDLE?
>
> No, just a misunderstanding of what [:] does, I presume.

You didn't answer my question, but I just realize that I
mis-typed my example, so never mind.

>
> --
> Gabriel Genellina

Mark Tolonen

2/8/2008 3:31:00 AM

0


<Matthew_WARREN@bnpparibas.com> wrote in message
news:mailman.468.1202405913.9267.python-list@python.org...
> Hallo,
>
> I'm after
>
> [[[],[],[],[],[]],[[],[],[],[],[]],[[],[],[],[],[]],[[],[],[],[],[]],[[],[],[],[],[]]]
>

How about:

>>> [[[]]*5]*5
[[[], [], [], [], []], [[], [], [], [], []], [[], [], [], [], []], [[], [],
[], [], []], [[], [], [], [], []]]

--Mark

Ben Finney

2/8/2008 4:10:00 AM

0

"Mark Tolonen" <mark.e.tolonen@mailinator.com> writes:

> <Matthew_WARREN@bnpparibas.com> wrote:
> > I'm after
> > [[[],[],[],[],[]],[[],[],[],[],[]],[[],[],[],[],[]],[[],[],[],[],[]],[[],[],[],[],[]]]
>
> How about:
>
> >>> [[[]]*5]*5
> [[[], [], [], [], []], [[], [], [], [], []], [[], [], [], [], []],
> [[], [], [], [], []], [[], [], [], [], []]]

Not too useful.

>>> foo = [[[]] * 5] * 5
>>> foo
[[[], [], [], [], []], [[], [], [], [], []], [[], [], [], [], []], [[], [], [], [], []], [[], [], [], [], []]]
>>> foo[0][0].append("spam")
>>> foo
[[['spam'], ['spam'], ['spam'], ['spam'], ['spam']], [['spam'], ['spam'], ['spam'], ['spam'], ['spam']], [['spam'], ['spam'], ['spam'], ['spam'], ['spam']], [['spam'], ['spam'], ['spam'], ['spam'], ['spam']], [['spam'], ['spam'], ['spam'], ['spam'], ['spam']]]

As made clear in the rest of the thread, the OP wants every one of
those lists to be different objects.

--
\ "To me, boxing is like a ballet, except there's no music, no |
`\ choreography, and the dancers hit each other." â??Jack Handey |
_o__) |
Ben Finney

Terry Reedy

2/8/2008 4:35:00 AM

0


"Mark Tolonen" <mark.e.tolonen@mailinator.com> wrote in message
news:r4GdnbsfRpVyUTbanZ2dnUVZ_uudnZ2d@comcast.com...
|
| <Matthew_WARREN@bnpparibas.com> wrote in message
| news:mailman.468.1202405913.9267.python-list@python.org...
| > Hallo,
| >
| > I'm after
| >
| >
[[[],[],[],[],[]],[[],[],[],[],[]],[[],[],[],[],[]],[[],[],[],[],[]],[[],[],[],[],[]]]
| >
|
| How about:
|
| >>> [[[]]*5]*5
| [[[], [], [], [], []], [[], [], [], [], []], [[], [], [], [], []], [[],
[],
| [], [], []], [[], [], [], [], []]]

Because that is actually only 1, not 25, empty lists
>>> a=[[[]]*5]*5
>>> a[0][0].append('ha')
>>> a
[[['ha'], ['ha'], ['ha'], ['ha'], ['ha']], [['ha'], ['ha'], ['ha'], ['ha'],
['ha']], [['ha'], ['ha'], ['ha'], ['ha'], ['ha']], [['ha'], ['ha'], ['ha'],
['ha'], ['ha']], [['ha'], ['ha'], ['ha'], ['ha'], ['ha']]]

tjr



mariano.suarezalvarez

2/8/2008 5:41:00 AM

0

On Feb 7, 3:38 pm, Matthew_WAR...@bnpparibas.com wrote:
> This message and any attachments (the "message") is
> intended solely for the addressees and is confidential.
> If you receive this message in error, please delete it and
> immediately notify the sender. Any use not in accord with
> its purpose, any dissemination or disclosure, either whole
> or partial, is prohibited except formal approval. The internet
> can not guarantee the integrity of this message.
> BNP PARIBAS (and its subsidiaries) shall (will) not
> therefore be liable for the message if modified.
> Do not print this message unless it is necessary,
> consider the environment.
>
> ---------------------------------------------
>
> Ce message et toutes les pieces jointes (ci-apres le
> "message") sont etablis a l'intention exclusive de ses
> destinataires et sont confidentiels. Si vous recevez ce
> message par erreur, merci de le detruire et d'en avertir
> immediatement l'expediteur. Toute utilisation de ce
> message non conforme a sa destination, toute diffusion
> ou toute publication, totale ou partielle, est interdite, sauf
> autorisation expresse. L'internet ne permettant pas
> d'assurer l'integrite de ce message, BNP PARIBAS (et ses
> filiales) decline(nt) toute responsabilite au titre de ce
> message, dans l'hypothese ou il aurait ete modifie.
> N'imprimez ce message que si necessaire,
> pensez a l'environnement.

Is this absurd signature really necessary?
It's only use is making it evident that the BNP IT team (or,
more probably, its legal department) has apparently
not heard about electronic signatures...

-- m

Ben Finney

2/8/2008 6:30:00 AM

0

"Mariano Suárez-Alvarez" <mariano.suarezalvarez@gmail.com> writes:

> On Feb 7, 3:38 pm, Matthew_WAR...@bnpparibas.com wrote:
> > [disclaimer dozens of lines long]
>
> Is this absurd signature really necessary?

No.

<URL:http://www.goldmark.org/jeff/stupid-discla...

--
\ "Never use a long word when there's a commensurate diminutive |
`\ available." â??Stan Kelly-Bootle |
_o__) |
Ben Finney

Steve Holden

2/8/2008 3:59:00 PM

0

Ben Finney wrote:
> "Mariano Suárez-Alvarez" <mariano.suarezalvarez@gmail.com> writes:
>
>> On Feb 7, 3:38 pm, Matthew_WAR...@bnpparibas.com wrote:
>>> [disclaimer dozens of lines long]
>> Is this absurd signature really necessary?
>
> No.
>
> <URL:http://www.goldmark.org/jeff/stupid-discla...
>
"Necessary" is dependent on control, of course, and I believe many
people are in the same situation as Matthew. If they want to use
corporate email they have to put up with the crap that their
ill-informed legal department makes the sysadmins configure the outgoing
mailer to add.

That particular trailing crud would be much nicer if they'd put a space
after the leading "--", that way at least we wouldn't have to trim them
out of our replies. This merely tells us that the sysadmins are as
ill-informed as the legal staff ... or perhaps it's just Matthew trying
his best to help us, since I see it appears above the list mailer
trailer. The system is probably trimming the trailing spaces off the
message body. Oh well, ...

regards
steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.hold...

Gabriel Genellina

2/8/2008 9:07:00 PM

0

En Fri, 08 Feb 2008 13:58:53 -0200, Steve Holden <steve@holdenweb.com>
escribió:

>> "Mariano Suárez-Alvarez" <mariano.suarezalvarez@gmail.com> writes:
>>> On Feb 7, 3:38 pm, Matthew_WAR...@bnpparibas.com wrote:
>>>> [disclaimer dozens of lines long]
>>> Is this absurd signature really necessary?

> That particular trailing crud would be much nicer if they'd put a space
> after the leading "--", that way at least we wouldn't have to trim them
> out of our replies. This merely tells us that the sysadmins are as
> ill-informed as the legal staff ... or perhaps it's just Matthew trying
> his best to help us, since I see it appears above the list mailer
> trailer. The system is probably trimming the trailing spaces off the
> message body. Oh well, ...

Perhaps he didn't know that a correct signature separator must be *exactly*
<newline><dash><dash><space><newline>
Nothing more, nothing less. It's easy to dismiss that last space - after
all, trailing spaces are almost never significant.

--
Gabriel Genellina