Well,
could some kind soul please explain to me why the following trivial code is
misbehaving?
#!/usr/bin/python
lst = [ 0, 1, 2 ]
s = []
l = [ lst[0] ]
r = lst[1:]
while r:
x = (l,r)
print x
s.append( x )
l.append( r.pop(0) )
print s
The output I get is:
([0], [1, 2])
([0, 1], [2])
[([0, 1, 2], []), ([0, 1, 2], [])]
and the error is in the last line: the two pairs in the outer list are
identical and they should be as the pairs on the first and the 2nd line,
respectively!
I think I'm going nuts -- for the life of me I don't see what's going on ...
(I've been tracking down a bug in my larger python script, and the cause
seems to boil down to the above snippet.)
Thanks a lot in advance for any insights, etc.
Best regards,
Gabriel. 3 1692
On 9/28/07, Gabriel Zachmann <za**@removeme.in.tu-clausthal.dewrote:
Well,
could some kind soul please explain to me why the following trivial code is
misbehaving?
#!/usr/bin/python
lst = [ 0, 1, 2 ]
s = []
l = [ lst[0] ]
r = lst[1:]
while r:
x = (l,r)
print x
s.append( x )
l.append( r.pop(0) )
print s
The output I get is:
([0], [1, 2])
([0, 1], [2])
[([0, 1, 2], []), ([0, 1, 2], [])]
and the error is in the last line: the two pairs in the outer list are
identical and they should be as the pairs on the first and the 2nd line,
respectively!
I think I'm going nuts -- for the life of me I don't see what's going on ...
(I've been tracking down a bug in my larger python script, and the cause
seems to boil down to the above snippet.)
Thanks a lot in advance for any insights, etc.
Best regards,
Gabriel.
-- http://mail.python.org/mailman/listinfo/python-list
If you're familiar with C or C++, think of s as holding a pointer to x
which in turn holds a pointer to l and r, so when you change l or r, x
(and s indirectly) is still pointing to the same lists which by the
end of your loop have changed to r=[] and l=[0,1,2].
BTW: It's not really "misbehaving." It's doing exactly what you're
telling it to do ;-)
Jason
If you're familiar with C or C++, think of s as holding a pointer to x
which in turn holds a pointer to l and r, so when you change l or r, x
(and s indirectly) is still pointing to the same lists which by the
AH - thanks a million -- that makes it crystal clear!
[Python's apparent simplicity keeps making me forget that everything is
a pointer ...]
BTW: It's not really "misbehaving." It's doing exactly what you're
telling it to do ;-)
i had a feeling ... ;-)
Cheers,
Gabriel.
--
__________________________________________________ ____________
Life is so constructed that the event does not, cannot,
will not match the expectation. (Charlotte Bronte)
__________________________________________________ ____________
zach in.tu-clausthal.de __@/' www.gabrielzachmann.org
__________________________________________________ ____________
x = (list(l), list(r))
BTW: I prefer this syntax, because it makes the copy explicit, while
l[:] seems to me more "implicit" ...
Best regards,
Gabriel.
--
__________________________________________________ ____________
Life is so constructed that the event does not, cannot,
will not match the expectation. (Charlotte Bronte)
__________________________________________________ ____________
zach in.tu-clausthal.de __@/' www.gabrielzachmann.org
__________________________________________________ ____________ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Todd MacCulloch |
last post by:
Suppose I have a master list and I need to create two derived lists,
something like:
s0 =
s1 =
s2 =
But suppose generating s0 is expensive and/or s0 is big. In otherwords I'd
like to go...
|
by: Dave H |
last post by:
Hello,
I have a query regarding definition lists. Is it good practice
semantically to use the dt and dd elements to mark up questions and
answers in a frequently asked questions list, or FAQ?
...
|
by: Christian Christmann |
last post by:
Hi,
in the past I always appreciated your help and hope that you also can help
me this time. I've spent many many hours but still can't solve the
problem by myself and you are my last hope.
...
|
by: Piotr Filip Mieszkowski |
last post by:
Hello,
I like both C++ and Lisp and sometimes try to mix their ideas in the
code I write. Recently I started to think about writing a pair class
similar to the CONS in Lisp. (For those of you...
|
by: Jonathan Burd |
last post by:
Greetings everyone,
I wrote a function to learn about variable-length argument lists. I
wonder if there is a better way to detect the end of the argument list
than using a sentinel value like...
|
by: Peter Kleiweg |
last post by:
This does not what I want it to do:
>>> a = ] * 6
>>> a.append('X')
>>> a
, , , , , ]
This does what I want:
>>> b = for _ in range(6)]
|
by: SMB |
last post by:
I have two lists of data like the following:
LIST1
, ]
LIST2
, 'label': 'First Name',
'width': 0L, 'separator': ',', 'height': 0L, 'type': 2L, 'order': 1L},
{'code': 14L, 'name': 'Last...
|
by: rshepard |
last post by:
I start with a list of tuples retrieved from a database table. These
tuples are extracted and put into individual lists. So I have lists that
look like this: . When I concatenate lists, I end up...
|
by: Gabriel Zachmann |
last post by:
Well,
could some kind soul please explain to me why the following trivial code
is misbehaving?
#!/usr/bin/python
s =
l =
r =
|
by: Aaron Watters |
last post by:
....is to forget they are sorted???
While trying to optimize some NUCULAR libraries I discovered
that the best way to merge 2 sorted lists together
into a new sorted list is to just append
them...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |