Sorting part of a list

S

Sibylle Koczian

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:
[3, 1, 2, 4]

_does_ change my original ll.

What did I misunderstand?

Thanks,
Sibylle
 
F

Fuzzyman

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)
 
J

John Machin

Sibylle said:
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

[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:
[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?
 
D

Dennis Lee Bieber

_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 in-place sort and return
of None. That would allow for:

ll[2:] = ll[2:].sorted()

--
 
S

Sibylle Koczian

Dennis said:
On Fri, 24 Jun 2005 13:42:39 +0200, Sibylle Koczian
<[email protected]> 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 in-place 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top