Sorting part of a list
Sibylle Koczian
Sibylle.Koczian at Bibliothek.Uni-Augsburg.de
Mon Jun 27 03:43:52 EDT 2005
Dennis Lee Bieber schrieb:
> On Fri, 24 Jun 2005 13:42:39 +0200, Sibylle Koczian
> <Sibylle.Koczian at Bibliothek.Uni-Augsburg.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 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
--
Dr. Sibylle Koczian
Universitaetsbibliothek, Abt. Naturwiss.
D-86135 Augsburg
e-mail : Sibylle.Koczian at Bibliothek.Uni-Augsburg.DE
More information about the Python-list
mailing list