P: n/a

Hello,
I thought I understood list slices, but I don't. I want to sort only the
last part of a list, preferably in place. If I do ll = [3, 1, 4, 2] ll[2:].sort() ll
[3, 1, 4, 2]
ll isn't changed, because ll[2:] is a copy of the last part of the list,
and this copy is sorted, not the original list. Right so far?
But assignment to the slice:
ll[2:] = [2, 4] ll
[3, 1, 2, 4]
_does_ change my original ll.
What did I misunderstand?
Thanks,
Sibylle

Dr. Sibylle Koczian
Universitaetsbibliothek, Abt. Naturwiss.
D86135 Augsburg
email : Si*************@Bibliothek.UniAugsburg.DE  
Share this Question
P: n/a

You can assign to a slice in place, but referencing a slice creates a
copy.
I think you understand it quite well :)
a = ll[2:]
a.sort()
ll[2:] = a
To do a partial sort, in place, you'll have to subclass list  but
you'll still end up doing something similar unless you delve into C or
write your own sort algorithm in Python. Neither are recommended.
Best Regards,
Fuzzy http://wwww.voidspace.org.uk/python
(back online hurrah)  
P: n/a

Sibylle Koczian wrote: Hello,
I thought I understood list slices, but I don't. I want to sort only the last part of a list, preferably in place. If I do
>>> ll = [3, 1, 4, 2] >>> ll[2:].sort()
It may help in unravelling any bogglement to point out that this is
equivalent to
temp = ll[2:]; temp.sort(); del temp
>>> ll [3, 1, 4, 2]
ll isn't changed, because ll[2:] is a copy of the last part of the list, and this copy is sorted, not the original list. Right so far?
Quite correct. But assignment to the slice: >>> ll[2:] = [2, 4] >>> ll [3, 1, 2, 4]
_does_ change my original ll.
Quite correct. What did I misunderstand?
What misunderstanding? You have described the behaviour rather
precisely. Which of the two cases is boggling you?  
P: n/a

Fuzzyman wrote: a = ll[2:] a.sort() ll[2:] = a
To do a partial sort, in place, you'll have to subclass list
Or be using 2.4: ll = [3, 1, 4, 2] ll[2:] = sorted(ll[2:]) ll
[3, 1, 2, 4]

Benji York  
P: n/a

On Fri, 24 Jun 2005 13:42:39 +0200, Sibylle Koczian
<Si*************@Bibliothek.UniAugsburg.de> declaimed the following in
comp.lang.python: _does_ change my original ll.
What did I misunderstand?
ll[2:].sort()
breaks down to:
newList = ll[2:]
newList.sort()
The slice is a "take" from the right hand side of an implied
assignment (create new object, sort it)
ll[2:] = ...
is not an object creation, merely an access into an existing object. ll = [3, 1, 4, 2] nl = ll[2:] # nl < newList nl.sort() ll[2:] = nl ll
[3, 1, 2, 4]
I'm still running 2.3.x  did "sorted()" make it into 2.4? As I
recall the discussion, the idea was to have a sort operation that would
return the sorted data, instead of the current inplace sort and return
of None. That would allow for:
ll[2:] = ll[2:].sorted()
 ================================================== ============ < wl*****@ix.netcom.com  Wulfraed Dennis Lee Bieber KD6MOG < wu******@dm.net  Bestiaria Support Staff < ================================================== ============ < Home Page: <http://www.dm.net/~wulfraed/> < Overflow Page: <http://wlfraed.home.netcom.com/> <  
P: n/a

Dennis Lee Bieber schrieb: On Fri, 24 Jun 2005 13:42:39 +0200, Sibylle Koczian <Si*************@Bibliothek.UniAugsburg.de> declaimed the following in comp.lang.python:
ll[2:] = ...
is not an object creation, merely an access into an existing object.
That's what I hadn't understood. Although it's the same with ll[2]. I'm still running 2.3.x  did "sorted()" make it into 2.4? As I recall the discussion, the idea was to have a sort operation that would return the sorted data, instead of the current inplace sort and return of None. That would allow for:
ll[2:] = ll[2:].sorted()
It's not a list method, but it's a function: ll = [3, 1, 4, 2] ll[2:] = ll[2:].sorted()
Traceback (most recent call last):
File "<pyshell#4>", line 1, in toplevel
ll[2:] = ll[2:].sorted()
AttributeError: 'list' object has no attribute 'sorted'
but this works:
ll = [3, 1, 4, 2] ll[2:] = sorted(ll[2:]) ll
[3, 1, 2, 4]
I'm using 2.4, but didn't look carefully at the changes. Thanks to all
who answered!
Sibylle

Dr. Sibylle Koczian
Universitaetsbibliothek, Abt. Naturwiss.
D86135 Augsburg
email : Si*************@Bibliothek.UniAugsburg.DE   This discussion thread is closed Replies have been disabled for this discussion.   Question stats  viewed: 1798
 replies: 5
 date asked: Jul 19 '05
