List of lists surprising behaviour

  • Permalink
  • submit to reddit
  • Email
  • Follow


Let's the following code :

 >>> t=[[0]*2]*3
 >>> t
[[0, 0], [0, 0], [0, 0]]
 >>> t[0][0]=1
 >>> t
[[1, 0], [1, 0], [1, 0]]

Rather surprising, isn't it ? So I suppose all the subarrays ref�rence 
the same array :

 >>> id(t[0]), id(t[1]), id(t[2])
(3077445996L, 3077445996L, 3077445996L)
 >>>


So what is the right way to initialize to 0 a 2D array ? Is that way 
correct  :


 >>> t=[[0 for _ in range(2)] for _ in range(3)]

It seems there is no more trouble now :

 >>> t
[[0, 0], [0, 0], [0, 0]]
 >>> t[0][0]=1
 >>> t
[[1, 0], [0, 0], [0, 0]]
 >>>

Correct ?
0
Reply candide (110) 6/17/2010 10:21:31 AM

See related articles to this posting


On 06/17/10 20:21, candide wrote:
> Let's the following code :
> 
>>>> t=[[0]*2]*3
>>>> t
> [[0, 0], [0, 0], [0, 0]]
>>>> t[0][0]=1
>>>> t
> [[1, 0], [1, 0], [1, 0]]
> 
> Rather surprising, isn't it ? So I suppose all the subarrays ref�rence
> the same array :
> 
>>>> id(t[0]), id(t[1]), id(t[2])
> (3077445996L, 3077445996L, 3077445996L)
>>>>

Yep, you're right. They share the same subarray if you uses
multiplication to build the array.

> So what is the right way to initialize to 0 a 2D array ? Is that way
> correct  :
>>>> t=[[0 for _ in range(2)] for _ in range(3)]

Right again. That's the way to go. Although if the elements are
immutable, you can create the innermost array by multiplication:

t=[[0]*2 for _ in range(3)]
0
Reply Lie 6/17/2010 10:59:52 AM

Yes you are. List comprehension makes you create list of lists without
reference-sharing. You should also find a recipe about that on the
python cookbook.

On Thu, Jun 17, 2010 at 12:21 PM, candide <candide@free.invalid> wrote:
> Let's the following code :
>
>>>> t=3D[[0]*2]*3
>>>> t
> [[0, 0], [0, 0], [0, 0]]
>>>> t[0][0]=3D1
>>>> t
> [[1, 0], [1, 0], [1, 0]]
>
> Rather surprising, isn't it ? So I suppose all the subarrays ref=E9rence =
the
> same array :
>
>>>> id(t[0]), id(t[1]), id(t[2])
> (3077445996L, 3077445996L, 3077445996L)
>>>>
>
>
> So what is the right way to initialize to 0 a 2D array ? Is that way corr=
ect
> =A0:
>
>
>>>> t=3D[[0 for _ in range(2)] for _ in range(3)]
>
> It seems there is no more trouble now :
>
>>>> t
> [[0, 0], [0, 0], [0, 0]]
>>>> t[0][0]=3D1
>>>> t
> [[1, 0], [0, 0], [0, 0]]
>>>>
>
> Correct ?
> --
> http://mail.python.org/mailman/listinfo/python-list
>



--=20
Matteo Landi
http://www.matteolandi.net/
0
Reply landimatte (24) 6/17/2010 11:11:25 AM

candide wrote:
>
> So what is the right way to initialize to 0 a 2D array ? Is that way
> correct :
>
>
>  >>> t=[[0 for _ in range(2)] for _ in range(3)]

That's overkill :) You can skip the inner loop by using a list display, eg

t=[[0,0] for _ in range(3)]

>
> It seems there is no more trouble now :
>
>  >>> t
> [[0, 0], [0, 0], [0, 0]]
>  >>> t[0][0]=1
>  >>> t
> [[1, 0], [0, 0], [0, 0]]
>  >>>
>
> Correct ?


0
Reply Boris 6/17/2010 1:56:32 PM

candide <candide@free.invalid> writes:

> Let's the following code :
>
>>>> t=[[0]*2]*3
>>>> t
> [[0, 0], [0, 0], [0, 0]]
>>>> t[0][0]=1
>>>> t
> [[1, 0], [1, 0], [1, 0]]
>
> Rather surprising, isn't it ? 

Not at all, actually.

I'd be surprised if the multiplication operator was aware of object
constructors.  Even arrays are "objects" in Python.  Should the
multiplication operator know how to instantiate three arrays from a
single array instance?  What about an instance of a user-defined class?

> So I suppose all the subarrays reférence
> the same array :
>
>>>> id(t[0]), id(t[1]), id(t[2])
> (3077445996L, 3077445996L, 3077445996L)
>>>>
>

As they should.

>
> So what is the right way to initialize to 0 a 2D array ? Is that way
> correct  :
>
>
>>>> t=[[0 for _ in range(2)] for _ in range(3)]
>
> It seems there is no more trouble now :
>
>>>> t
> [[0, 0], [0, 0], [0, 0]]
>>>> t[0][0]=1
>>>> t
> [[1, 0], [0, 0], [0, 0]]
>>>>
>
> Correct ?

>>> 2d_zero_vector = lambda len: [[0, 0] for _ in range(len)]
>>> t = 2d_zero_vector(3)
>>> print t
[[0, 0], [0, 0], [0, 0]]
>>> t[0][0] = 1
>>> print t
[[1, 0], [0, 0], [0, 0], [0, 0]]

(Of course, if you're doing matrix math you'll probably want to work
with numpy which has a function for doing just this)
0
Reply J 6/17/2010 8:25:53 PM

"J Kenneth King" <james@agentultra.com> wrote in message
news:87wrtxh0dq.fsf@agentultra.com...
> candide <candide@free.invalid> writes:
>
>> Let's the following code :
>>
>>>>> t=[[0]*2]*3
>>>>> t
>> [[0, 0], [0, 0], [0, 0]]
>>>>> t[0][0]=1
>>>>> t
>> [[1, 0], [1, 0], [1, 0]]
>>
>> Rather surprising, isn't it ?
>
> Not at all, actually.

The code is clearly trying to set only t[0][0] to 1, not t[1][0] and t[2][0]
as well.

This behaviour is quite scary actually, especially when t[0]=42 *does* work
as expected, while t[0][0]=42 is apparently duplicated. It appears
inconsistent.

> I'd be surprised if the multiplication operator was aware of object
> constructors.  Even arrays are "objects" in Python.  Should the
> multiplication operator know how to instantiate three arrays from a
> single array instance?  What about an instance of a user-defined class?

Multiplication operators shouldn't need to be directly aware of any such 
thing; it should just request that an object be duplicated without worrying 
about how it's done.

I don't know how Python does things, but an object should either specify a 
special way of duplicating itself, or lend itself to some standard way of 
doing so. (So for a list, it's just a question of copying the data in the 
list, then recursively duplicating each new element..)

-- 
Bartc

0
Reply bart 6/17/2010 11:20:30 PM

On Thu, Jun 17, 2010 at 4:20 PM, bart.c <bartc@freeuk.com> wrote:
>
> "J Kenneth King" <james@agentultra.com> wrote in message
> news:87wrtxh0dq.fsf@agentultra.com...
>>
>> candide <candide@free.invalid> writes:
>>
>>> Let's the following code :
>>>
>>>>>> t=3D[[0]*2]*3
>>>>>> t
>>>
>>> [[0, 0], [0, 0], [0, 0]]
>>>>>>
>>>>>> t[0][0]=3D1
>>>>>> t
>>>
>>> [[1, 0], [1, 0], [1, 0]]
>>>
>>> Rather surprising, isn't it ?
>>
>> Not at all, actually.
>
> The code is clearly trying to set only t[0][0] to 1, not t[1][0] and t[2]=
[0]
> as well.
>
> This behaviour is quite scary actually, especially when t[0]=3D42 *does* =
work
> as expected, while t[0][0]=3D42 is apparently duplicated. It appears
> inconsistent.
>
>> I'd be surprised if the multiplication operator was aware of object
>> constructors. =A0Even arrays are "objects" in Python. =A0Should the
>> multiplication operator know how to instantiate three arrays from a
>> single array instance? =A0What about an instance of a user-defined class=
?
>
> Multiplication operators shouldn't need to be directly aware of any such
> thing; it should just request that an object be duplicated without worryi=
ng
> about how it's done.
>
> I don't know how Python does things, but an object should either specify =
a
> special way of duplicating itself, or lend itself to some standard way of
> doing so. (So for a list, it's just a question of copying the data in the
> list, then recursively duplicating each new element..)
>
> --
> Bartc

It's the recursively duplicating each element that's the problem. How
do you know when to stop?
0
Reply Benjamin 6/17/2010 11:44:49 PM

On Jun 17, 6:44=A0pm, Benjamin Kaplan <benjamin.kap...@case.edu> wrote:

> It's the recursively duplicating each element that's the problem. How
> do you know when to stop?


Thats easy, stack overflow! ;-)
0
Reply rantingrick 6/18/2010 12:48:06 AM

On 06/18/10 09:20, bart.c wrote:
> 
> "J Kenneth King" <james@agentultra.com> wrote in message
> news:87wrtxh0dq.fsf@agentultra.com...
>> candide <candide@free.invalid> writes:
>>
>>> Let's the following code :
>>>
>>>>>> t=[[0]*2]*3
>>>>>> t
>>> [[0, 0], [0, 0], [0, 0]]
>>>>>> t[0][0]=1
>>>>>> t
>>> [[1, 0], [1, 0], [1, 0]]
>>>
>>> Rather surprising, isn't it ?
>>
>> Not at all, actually.
> 
> The code is clearly trying to set only t[0][0] to 1, not t[1][0] and
> t[2][0]
> as well.
> 
> This behaviour is quite scary actually, especially when t[0]=42 *does* work
> as expected, while t[0][0]=42 is apparently duplicated. It appears
> inconsistent.

I agree, the behavior is often quite inconvenient, but I disagree that
it is inconsistent. List multiplication behavior is consistent with the
tenet: "objects are never copied unless explicitly requested"

Peeking further:
t = [[0] * 2] * 3
print id(t[0]) == id(t[1])  # True
print id(t[0][0]) == id(t[1][0])  # True

so, it is consistent (though it is quite inconvenient).

>> I'd be surprised if the multiplication operator was aware of object
>> constructors.  Even arrays are "objects" in Python.  Should the
>> multiplication operator know how to instantiate three arrays from a
>> single array instance?  What about an instance of a user-defined class?
> 
> Multiplication operators shouldn't need to be directly aware of any such
> thing; it should just request that an object be duplicated without
> worrying about how it's done.
> 
> I don't know how Python does things, but an object should either specify
> a special way of duplicating itself, or lend itself to some standard way
> of doing so. (So for a list, it's just a question of copying the data in
> the list, then recursively duplicating each new element..)

That is inconsistent with the tenet. Moreover, the implicit copying
makes it quite difficult to reason about the program's behavior. How
would you propose this list should be copied:

class O(object):
    def __init__(self):
        self.attr = 0
    def add(self):
        self.attr += 1

b = [[O()] * 2] * 3
0
Reply Lie 6/18/2010 1:38:59 AM

On Fri, 18 Jun 2010 00:20:30 +0100, bart.c wrote:

> The code is clearly trying to set only t[0][0] to 1, not t[1][0] and
> t[2][0] as well.

Trying to guess the motivation of the person writing code is tricky, but 
in this case, that's a reasonable assumption. I can't think of any reason 
why somebody would explicitly *want* that behaviour:

# Does it make sense to talk of anonymous aliases?
list_of_aliases = [[0]*2]*3
list_of_aliases[0][0] = 1
assert list_of_aliases[0][1] == 1

so it is a safe guess that anyone writing [[0]*2]*3 has probably made a 
mistake.

However, I've certainly done something like this:

a = [0]*2
my_instance.items = a
a[0] = 1
assert my_instance.items[0] = 1

This is the same fundamental behaviour with the same cause: Python does 
not copy objects unless you explicitly tell it to.

I cheerfully accept that the behaviour of [[0]*2]*3 is a Gotcha, but it 
follows logically from Python's object model and assignment rules. If you 
are surprised by it, it just goes to show that your understanding of 
Python has at least one hole in it.

 
> This behaviour is quite scary actually, especially when t[0]=42 *does*
> work as expected, while t[0][0]=42 is apparently duplicated. It appears
> inconsistent.

Emphasis on the word "appears". It actually displays a deep consistency 
with the language fundamentals.

If you're ever interviewing somebody for a position as Python developer, 
this is a quick test to distinguish those who know the language from 
those who know the language *well*.


>> I'd be surprised if the multiplication operator was aware of object
>> constructors.  Even arrays are "objects" in Python.  Should the
>> multiplication operator know how to instantiate three arrays from a
>> single array instance?  What about an instance of a user-defined class?
> 
> Multiplication operators shouldn't need to be directly aware of any such
> thing; it should just request that an object be duplicated without
> worrying about how it's done.

The multiplication operator is not a duplicator (copier). It is a 
*repetition* operator: repeat the object N times, not make N copies.


> I don't know how Python does things, 

Well there you go :)


> but an object should either specify
> a special way of duplicating itself, or lend itself to some standard way
> of doing so.

import copy
copy.copy(obj)

Dicts have a copy() method as a shortcut, and for lists you can use 
slicing:

L = [1,2,3]
Lcopy = L[:]


> (So for a list, it's just a question of copying the data in
> the list, then recursively duplicating each new element..)

There's nothing "just" about that. Consider:

L = [1, 2]
L.append(L)

How would you copy that?

The correct answer is:

x = copy.deepcopy(L)



-- 
Steven
0
Reply Steven 6/18/2010 5:36:33 AM

Benjamin Kaplan wrote:
> On Thu, Jun 17, 2010 at 4:20 PM, bart.c <bartc@freeuk.com> wrote:

>> I don't know how Python does things, but an object should either
>> specify a special way of duplicating itself, or lend itself to some
>> standard way of doing so. (So for a list, it's just a question of
>> copying the data in the list, then recursively duplicating each new
>> element..)

> It's the recursively duplicating each element that's the problem. How
> do you know when to stop?

When you reach a primitive object (one not comprising other objects). (I 
don't know if Python allows circular references, but that would give 
problems anyway: how would you even print out such a list?)

-- 
Bartc 

0
Reply bart 6/18/2010 10:00:39 AM

On 06/18/10 20:00, bart.c wrote:
> (I
> don't know if Python allows circular references, but that would give
> problems anyway: how would you even print out such a list?)


Python uses ellipsis to indicate recursive list:

>>> a = [1, 2, 3]
>>> a.append(a)
>>> a
[1, 2, 3, [...]]
0
Reply Lie 6/18/2010 10:46:07 AM

Lie Ryan wrote:
> On 06/18/10 20:00, bart.c wrote:
>> (I
>> don't know if Python allows circular references, but that would give
>> problems anyway: how would you even print out such a list?)
>
>
> Python uses ellipsis to indicate recursive list:
>
>>>> a = [1, 2, 3]
>>>> a.append(a)
>>>> a
> [1, 2, 3, [...]]

Ok, perhaps whatever logic print uses to know when to stop and show "...", 
can also be used by copy handlers...

(Although I have an issue with the way that that append works. I tried it in 
another, simpler language (which always does deep copies):

L:=(1,2,3)
L append:= L
print L

output:  (1,2,3,(1,2,3))

which is exactly what I'd expect, and not (1,2,3,(1,2,3,(1,2,3,...))) )

-- 
bartc 

0
Reply bart 6/18/2010 11:07:38 AM

On Fri, 18 Jun 2010 12:07:38 +0100, bart.c wrote:

> (Although I have an issue with the way that that append works. I tried
> it in another, simpler language (which always does deep copies):
> 
> L:=(1,2,3)
> L append:= L
> print L
> 
> output:  (1,2,3,(1,2,3))
> 
> which is exactly what I'd expect, 
> and not (1,2,3,(1,2,3,(1,2,3,...))) )

I find that behaviour a big surprise. You asked to append the list L, not 
a copy of the list L. So why is this "simpler" language making a copy 
without being asked?

If you asked for:

L:=(1,2,3)
M:=(0,1)
M append:= L

does it also append a copy of L instead of L? If so, how do you append 
the original rather than wastefully making a copy? If L is huge, making a 
copy before appending will be slow, and potentially fail.



-- 
Steven
0
Reply Steven 6/18/2010 3:03:34 PM

"Steven D'Aprano" <steve@REMOVE-THIS-cybersource.com.au> wrote in message
news:4c1b8ac6$0$14148$c3e8da3@news.astraweb.com...
> On Fri, 18 Jun 2010 12:07:38 +0100, bart.c wrote:
>
>> (Although I have an issue with the way that that append works. I tried
>> it in another, simpler language (which always does deep copies):
>>
>> L:=(1,2,3)
>> L append:= L
>> print L
>>
>> output:  (1,2,3,(1,2,3))
>>
>> which is exactly what I'd expect,
>> and not (1,2,3,(1,2,3,(1,2,3,...))) )
>
> I find that behaviour a big surprise. You asked to append the list L, not
> a copy of the list L. So why is this "simpler" language making a copy
> without being asked?
>
> If you asked for:
>
> L:=(1,2,3)
> M:=(0,1)
> M append:= L
>
> does it also append a copy of L instead of L?

It make a copy.

> If so, how do you append
> the original rather than wastefully making a copy?

I don't think it can, without perhaps doing something with explicit
pointers.

> If L is huge, making a
> copy before appending will be slow, and potentially fail.

I don't know whether L append:=L requires 3 times the space of L, or 2
times, during the operation. But it should be doable using just twice the
space.

I suppose there are pros and cons to both approaches; copying all the time
at least avoids some of the odd effects and inconsistencies you get using
Python:

a1=[1,2,3]
a1.append(a1)

a2=[1,2,3]
b=[1,2,3]
a2.append(b)

a3=[1,2,3]
a3.append([1,2,3])

print ("a1 = ",a1)
print ("a2 = ",a2)
print ("a3 = ",a3)

Here, a1 ends up with a different result from a2, a3, even though the same
value is being appended to the same list of numbers in each case. And it 
might sometimes bite you in the arse as the OP demonstrated:

L=[1,2,3]
M=[0,1]
M.append(L)

print (M)     # output: [0, 1, [1, 2, 3]]

L[1]=31416

print (M)     # output: [0, 1, [1, 31416, 3]], yikes!

-- 
Bartc


0
Reply bartc (786) 6/18/2010 3:40:35 PM

This is an OpenPGP/MIME signed message (RFC 2440 and 3156)
--------------enig3BB2A9F215CDFED19118EACA
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

On 6/18/10 8:40 AM, bart.c wrote:
> I suppose there are pros and cons to both approaches; copying all the t=
ime
> at least avoids some of the odd effects and inconsistencies you get usi=
ng
> Python:

What inconsistencies? All your examples are perfectly consistent. Its
just consistent to different ideals. Python never copies implicitly; and
every object you create has a concrete identity in and of itself,
utterly separate from its potential equality.

> a1=3D[1,2,3]
> a1.append(a1)

The "a1" object is a distinct object unto itself; you are appending said
distinct object onto the end of itself. Entirely doable, even if you
don't usually want to. Recursive, but doable if that's what you want. If
you wished to append a copy, you must-- as always, consistently--
explicitly copy it.

I.e.,

a1.append(a1[:])

> a2=3D[1,2,3]
> b=3D[1,2,3]
> a2.append(b)

a2 is a distinct object from b; that the two objects are equal means
nothing. So you're appending an object to the end of a2 which happens to
be equal to it.

> a3=3D[1,2,3]
> a3.append([1,2,3])

This is just another way of writing the previous example; that in one
you are naming the object [1,2,3] and in the other you are not, doesn't
mean anything. A named vs unnamed object in Python behaves exactly the sa=
me.

> print ("a1 =3D ",a1)
> print ("a2 =3D ",a2)
> print ("a3 =3D ",a3)
>=20
> Here, a1 ends up with a different result from a2, a3, even though the s=
ame
> value is being appended to the same list of numbers in each case.=20

There's the rub: the VALUE is not being appended. The *actual object* is.=


Consider:

>>> print a1 is a2
False

> And it
> might sometimes bite you in the arse as the OP demonstrated:
>=20
> L=3D[1,2,3]
> M=3D[0,1]
> M.append(L)
>=20
> print (M)     # output: [0, 1, [1, 2, 3]]
>=20
> L[1]=3D31416
>=20
> print (M)     # output: [0, 1, [1, 31416, 3]], yikes!

That might bite you on your arse if you think Python implicitly copies;
but since the rule is very simple -- it /never/ implicitly copies -- and
that it objects are discrete entities and not just names of certain
values, it more likely then not will be beneficial to you quite often
down the road. You'll -want- that to be how things work. Eventually.
When you learn more Python.

--=20

   Stephen Hansen
   ... Also: Ixokai
   ... Mail: me+list/python (AT) ixokai (DOT) io
   ... Blog: http://meh.ixokai.io/


--------------enig3BB2A9F215CDFED19118EACA
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.10 (Darwin)

iQEcBAEBAgAGBQJMG6MqAAoJEKcbwptVWx/ll/YIALfJMaQuxVQGeF0EXGllNGl4
zAH5akcpA3nFr+X6E2I1Xc9MxN162dIbBuRJ6GrLwej2wyklkgm3snMDCQAUA0QU
xGEzyIma0Mi6KotydyEkmpgxM6b3vQj8SSlmM6mex+zKgR+HlIXvl/zapyPc9v+8
Kfc8glJgoFXmiv7W9fbwCu8g/w6KeIiAaaC7JepMwm6IoVsrP888p65Add8eEtKb
PUQKuU1bSEnysmCVstzUnY4lIsByOW3ygpnnA6++uDeR3AwZ4sLRN0J33UsUiGJM
kPV6c6LkJ9+jY3dObbnSusVPcdfvOIyUlN8i4nxKb/JddPQ9aTptLO/kupZm8xA=
=18Pu
-----END PGP SIGNATURE-----

--------------enig3BB2A9F215CDFED19118EACA--
0
Reply python3307 (206) 6/18/2010 4:47:38 PM
comp.lang.python 72721 articles. 12 followers. Post

15 Replies
188 Views

Similar Articles

[PageSpeed] 57


  • Permalink
  • submit to reddit
  • Email
  • Follow


Reply:

Similar Artilces:

List of lists of lists of lists...
I would like to have a list of lists N times deep, and my solution is (in pseudocode): def deep(x): a=[x] return a mylist=[] for N: mylist=deep(mylist) Is there a more elegant way to do it? The maine idea is: from a list having the numbre of steps along N dimensions, generate a list with an item at each possible point. Example 1: N=2 list=[2,3] result=[[1,2],[1,2],[1,2]] Example 2: N=3 list=[3,1,2] result=[[[1,2,3]],[[1,2,3]]] -- Ángel Gutiérrez Rodríguez - agr@fq.uniovi.es Instituto de Ciencia de los Materiales de Madrid - CSIC SpLine - European Syncrothorn Radiat...

list of lists of lists ....
Hi, I have a list of data (type A) my list can includes element of type A or a lists, these list can includes element of type A or a lists, and so on ... is there a simple way to obtain a single list of all the elemets of type A ? thanks yomgui I forgot the most important, I am looking for a non recursive method. thanks yomgui yomgui wrote: > > Hi, > > I have a list of data (type A) > my list can includes element of type A or a lists, > these list can includes element of type A or a lists, and so on ... > > is there a simple way to obtain a single list of all...

Sort a List, in a List of Lists of Lists
Dear Mathgroup, I have a lsit of Lists of Lists: {{{1,2},{2,1},{1,1}},{{1,1},{1,1},{1,2}},{{2,1},{2,2},{1,2}},{{2,2},{1,2},{2,2}},{{1,1},{2,1},{1,2}},{{1,2},{2,2},{2,2}}} I would like to sort the elements in the lowest level of brackets to give {{{1, 2}, {1, 2}, {1, 1}}, {{1, 1}, {1, 1}, {1, 2}}, {{1, 2}, {2, 2}, {1, 2}}, {{2, 2}, {1, 2}, {2, 2}}, {{1, 1}, {1, 2}, {1, 2}}, {{1, 2}, {2, 2}, {2, 2}}} i.e retaining the same structure with the paired elements in the original order. I can't seem to get the syntax right to do this apart from the obvious {{Sort[{1,...

List behaviour
Maybe I'm missing something but the latter is not the behaviour I'm expecting: >>> a = [[1,2,3,4], [5,6,7,8]] >>> b = a[:] >>> b [[1, 2, 3, 4], [5, 6, 7, 8]] >>> a == b True >>> a is b False >>> for i in range(len(b)): .... for x in range(4): .... b[i][x] = b[i][x] + 10 .... >>> b [[11, 12, 13, 14], [15, 16, 17, 18]] >>> a [[11, 12, 13, 14], [15, 16, 17, 18]] >>> ActivePython 2.4.2 Build 248 (ActiveState Corp.) based on Python 2.4.2 (#67, Oct 30 2005, 16:11:18) [MSC v.1310 32 bit (Intel)] o...

surprising += for lists
Hi everybody! I was just smacked by some very surprising Python 2.7 behaviour. I was assembling some 2D points into a list: points = [] points += (3, 5) points += (4, 6) What I would have expected is to have [(3, 5), (4, 6)], instead I got [3, 5, 4, 6]. My interpretations thereof is that the tuple (x, y) is iterable, so the elements are appended one after the other. Actually, I should have used points.append(), but that's a different issue. Now, what really struck me was the fact that [] + (3, 5) will give me a type error. Here I wonder why the augmented assignme...

Re: Sort a List, in a List of Lists of Lists
On 11/13/10 at 12:59 AM, leigh.pascoe@inserm.fr wrote: >I have a lsit of Lists of Lists: >{{{1,2},{2,1},{1,1}},{{1,1},{1,1},{1,2}},{{2,1},{2,2},{1,2}},{{2,2}, >{1,2},{2,2}},{{1,1},{2,1},{1,2}},{{1,2},{2, 2},{2,2}}} >I would like to sort the elements in the lowest level of brackets to >give >{{{1, 2}, {1, 2}, {1, 1}}, {{1, 1}, {1, 1}, {1, 2}}, {{1, 2}, {2, >2}, {1, 2}}, {{2, 2}, {1, 2}, {2, 2}}, {{1, 1}, {1, 2}, {1, 2}}, >{{1, 2}, {2, 2}, {2, 2}}} >i.e retaining the same structure with the paired elements in the >original order. I can't seem...

List of Atom or List of List??
how can detect some element of list is list o Atom?? for example when i try head (head p1) where p1=[[1,2],2] return 1 but when try head (head p1) where p1=[1,2] it returns Error plz help me..it my project and i have just 2 hours..:( i want to replace on Atom by another in list..for example: replace 1 2 [1,2] => [2,2] but sometime i have replace 1 2 [[1,2],2]=>[[2,2],2] i want to detect if first element of list is list then recurcively call head on that element.. is there any solutions?? On 8 Apr 2007 02:46:25 -0700, kheirandish.amin@gmail.com wrote: >how can detect some eleme...

convert list of lists to list
Is there a way to convert list_of_listsA to list_of_listsB, where one list in listof lists A is one element of listB? list_of_listsA: [['klas*', '*', '*'], ['mooi*', '*', '*', '*'], ['koe'], ['arm*', '*', '*(haar)'], ['groei*', '*', '*', '*', '*']] listB: ['klas* * *', 'mooi* * * *, 'koe', 'arm* * * (haar)', 'groei* * * * *'] Thankx! antar2 wrote: > Is there a way to convert list_of_listsA to list_of_listsB, where one >...

Extracting a List from a List of lists
Hi, I have an ArrayList of ArrayLists. I want to extract all the lists, but I dont know how many ArrayLists will be in the ArrayList. I know I can do it if i know how many lists are there using the ArrayList get() method. this is how i'm doing it List<String> list1 = new ArrayList<String>(); list1 = res.get(0); List<String> list1 = new ArrayList<String>(); list2 = res.get(1); List<String> list1 = new ArrayList<String>(); list3 = res.get(2); But if theres only two lists in the list i get a NullPointerException Is there any way i can loop through the l...

How to have a list of lists (or array of lists)
Hi, I want to have many lists, such as list0, list1, list2, ..., each one holding different number of items. Is there something like list[0] list[1] list[2] so that I can iterate through this list of lists? Thanks! bahoo On Apr 3, 7:12 pm, "bahoo" <b83503...@yahoo.com> wrote: > Hi, > > I want to have many lists, such as list0, list1, list2, ..., each one > holding different number of items. > Is there something like > list[0] > list[1] > list[2] > > so that I can iterate through this list of lists? > > Thanks! > bahoo listOfLists = [...

Copying a List to a List of Lists
Hi, I am having trouble with the following: I wish to have a list of lists of type Double called A. I then have a separate List of Doubles called B which i wish to add to A. I then want to be able to clear B and reuse it without clearing what I have added to A. Currently my code looks like: for(int i=0;i<seqLength;i++) { B.clear(); for(int j=i+1;j<seqLength;j++) { if(fourGameteTest(i,j)) { B.add(segPositions.get(j)); } } A.get(i).add(B); } However, it seems that due to, I guess the element held in A being a reference to the same pl...

lists of lists
I'm using C++ and I'm trying to create a list of a list and it won't let me create an iterator for it. if I do a list of an int, everything is fine. my syntax is list<list<string> > some_list; and my iterator would be someting like list<list>::Iterator blah; or maybe list<list<string> >::Iterator blah; ?? neither work, ofcourse. basicaly I'm trying to parse a text file, each line is token that contains tokens and I have a function that takes a string and returns the list of the tokens. so I have my the parent list to contain lists to t...

Lists of list
Hi All I am having problem with delete line if its belong to another one , example ['0132442\n', '13\n', '24\n'] the 2nd and 3rd are already in the first line , how can do this !!! Thanks Mohammed Altaj wrote: > Hi All > > I am having problem with delete line if its belong to another one , example I think, you mean to remove all lines that are substrings of another line. l = ['0132442\n', '13\n', '24\n'] l = [e.strip() for e in l] i = 0 while True: try: for j in range(len(l)): if i == j: continue if l[j]...

list*list
There must be a better way to multiply the elements of one list by another: a = [1,2,3] b = [1,2,3] c = [] for i in range(len(a)): c.append(a[i]*b[i]) a = c print a [1, 4, 9] Perhaps a list comprehension or is this better addressed by NumPy? Thanks, jab > There must be a better way to multiply the elements of one list by > another: > > a = [1,2,3] > b = [1,2,3] > c = [] > for i in range(len(a)): > c.append(a[i]*b[i]) > a = c > print a > [1, 4, 9] > > Perhaps a list comprehension or is this better addressed by NumPy? First of all: it's ...

When is a List not a List?
g[x_, n_] := x^n FullForm[Table[g[x, n], {n, 1, 2}]] FullForm[{g[x, 1], g[x, 2]}] Plot[{g[x, 1], g[x, 2]}, {x, 0, 1}, PlotStyle -> {Red, Blue}] Plot[Table[g[x, n], {n, 1, 2}], {x, 0, 1}, PlotStyle -> {Red, Blue}] The FullForm[]s are identical. One Plot[] has red and blue curves; the other has two blue curves. Quirky! AES wrote: > g[x_, n_] := x^n > FullForm[Table[g[x, n], {n, 1, 2}]] > FullForm[{g[x, 1], g[x, 2]}] > Plot[{g[x, 1], g[x, 2]}, {x, 0, 1}, PlotStyle -> {Red, Blue}] > Plot[Table[g[x, n], {n, 1, 2}], {x, 0,...

List of lists...
I would like to add a List environment with an accompanying list of lists included at the front of the document. Basically, a List is like a table or a figure. However, I want to be able to refer to a bulleted list. I am using gatech-thesis.cls (which is based on report.cls). I went through report.cls, and copied everything having to do with figures into gatech-thesis.cls (just after it includes report.cls) and changed everything that said figure to list. Then, I went through gatech-thesis.cls, and copied everything having to do with figures, changing everything that said figure to list.....

List of lists
How do I extract a list of lists from a user defined function and print the results as strings for each list? On Fri, 27 Jun 2003 01:44:42 GMT, Mike wrote: > How do I extract a list of lists from a user defined function Depends how that function is returning its values. The most obvious, and simplest way to do what you describe, is to have the function return a list of lists, as its return value. If you mean something else, you'll have to be more descriptive of what you actually want to do. > and print the results as strings for each list? The 'repr()' method of any o...

List of lists
So you can create listoffigures tableofcontents and listoftables, but can you and if so how do you do the same thing for a list of numbered lists? Cheers Try the tocloft package: http://www.ctan.org/tex-archive/help/Catalogue/entries/tocloft.html On 21 Mar, 17:26, "Matt" <matthew.w.tur...@gmail.com> wrote: > Try the tocloft package: > > http://www.ctan.org/tex-archive/help/Catalogue/entries/tocloft.html That looks great, cheers. But I am having trouble using it. This is the code I am trying, just lifted from the instructions: \newcommand{\listanswername}{List o...

List of a list
Hi, I'm very new to latex. I'm trying to make a list of a list, with: \begin{list}{} \item \begin{list}{} \item \end{list} \end{list} However, I'm getting the error: Runaway argument? ! Paragraph ended before \list was complete. <to be read again> \par l.2273 However, I can do a list of enumerates, but that's not what I want. Is this a clash in syntax? Is there a way to prevent this? Please help. Michael On 1 Nov 2004 00:19:43 -0800, Michael <dayzman@hotmail.com> wrote: > Hi, > I'm very new to latex. I'm trying to make a list ...

list or not a list?
Hello, I was a bit astonished using such tcl code: % set list "Abbruch der Aktion \" ##ACTION \" !" Abbruch der Aktion " ##ACTION " ! % llength $list 5 % llength [split $list] 7 % % set idx 0 0 % foreach element $list {puts "element($idx) = '$element'"; incr idx} element(0) = 'Abbruch' element(1) = 'der' element(2) = 'Aktion' element(3) = ' ##ACTION ' element(4) = '!' % % set idx 0 0 % foreach element [split $list] {puts "ele...

Lists from Lists
Can anyone tell me how can I make a list from all the first elements of the following pairs? l:=[[1,2],[3,4],[5,6],[7,8]] ; to get, m:=[1,3,5,7]; Thanks, Tony Connell "Tony Connell" <a.connell@galileo.karoo.co.uk> wrote in message news:vfk59prj7bp0fb@corp.supernews.com... > Can anyone tell me how can I make a list from all the first elements of the > following pairs? > > l:=[[1,2],[3,4],[5,6],[7,8]] ; > > to get, > > m:=[1,3,5,7]; > If you know the length of the list you could do like this: > m:=[seq(l[i][1], ...

Lists of lists
I've written a simple forum. The posts are lists of lists of lists... Each new list steps out the left margin. What happens is that after so many replies the lists get pushed too far right. I'd like to reset the list margin back to the beginning. for ex: ul > ul>ul> ul > ul... { margin-left: original thread left position; ??? position: absolute; left 0; } For ex: http://earlymorningreport.com/test_zig_zag_lists.html I would think this would not be an uncommon request. How now can we do this (other than that)? Jeff In article <jpom3b...

cons and list behaviour
Please excuse me if this has been addressed before. I searched the recent posts and the FAQ and did not see find the answer I am seeking. I am learnin LISP (actually elisp ;)), and have written the following: ;; here I am using cons notation (setq node-1 (cons 'a 'b)) (setq node-2 (cons 'c 'd)) (setq node-3 (cons 'e node-1)) (setq node-4 (cons node-3 'f)) (setq node-5 (cons node-2 'g)) (setq tree (cons node-4 node-5)) ;; (((e a . b) . f) (c . d) . g) <<<<< value ;; here we can see that a cons is *not* the same as a list (setq tree2 (copy-tree tree)...

FTP listing behaviour
Hi all, I have a user who regularly uses Internet Explorer (IE5) to ftp to one of my AIX 4.3.3 boxes. This is fine and has worked with no issues for years. Since migrating some of the apps to an AIX 5.2 box the ftp behaviour exhibited by IE5 has changed. What happens now is that the file name or directory name in the first column of the display is prefixed by the file date or time. Thus a file appears like this, for example: 11:04 smit.log Additionally, the "Modified" column in IE5, which should show the file date/time only shows a constant "01/01/1980 12:00" Obviou...