473,385 Members | 1,279 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Sorting part of a list

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.
D-86135 Augsburg
e-mail : Si*************@Bibliothek.Uni-Augsburg.DE
Jul 19 '05 #1
5 2167
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)

Jul 19 '05 #2
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?
Jul 19 '05 #3
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
Jul 19 '05 #4
On Fri, 24 Jun 2005 13:42:39 +0200, Sibylle Koczian
<Si*************@Bibliothek.Uni-Augsburg.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 in-place 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/> <

Jul 19 '05 #5
Dennis Lee Bieber schrieb:
On Fri, 24 Jun 2005 13:42:39 +0200, Sibylle Koczian
<Si*************@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 : Si*************@Bibliothek.Uni-Augsburg.DE
Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in Python, use the “sort” method. For example: ...
4
by: FBM | last post by:
Hi, I am working on a program that simulates one of the elements of ATM. The simulation stores events which occurs every some milliseconds for a certain amount of time. Every time that an event...
5
KevinADC
by: KevinADC | last post by:
Introduction This discussion of the sort function is targeted at beginners to perl coding. More experienced perl coders will find nothing new or useful. Sorting lists or arrays is a very common...
1
KevinADC
by: KevinADC | last post by:
Introduction In part one we discussed the default sort function. In part two we will discuss more advanced techniques you can use to sort data. Some of the techniques might introduce unfamiliar...
3
KevinADC
by: KevinADC | last post by:
If you are entirely unfamiliar with using Perl to sort data, read the "Sorting Data with Perl - Part One and Two" articles before reading this article. Beginning Perl coders may find this article...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.