473,807 Members | 2,886 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Universitaetsbi bliothek, Abt. Naturwiss.
D-86135 Augsburg
e-mail : Si************* @Bibliothek.Uni-Augsburg.DE
Jul 19 '05 #1
5 2187
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 misunderstandin g? 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.Un i-Augsburg.de> declaimed the following in
comp.lang.pytho n:

_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.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
=============== =============== =============== =============== == <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.ne tcom.com/> <

Jul 19 '05 #5
Dennis Lee Bieber schrieb:
On Fri, 24 Jun 2005 13:42:39 +0200, Sibylle Koczian
<Si************ *@Bibliothek.Un i-Augsburg.de> declaimed the following in
comp.lang.pytho n:
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
Universitaetsbi bliothek, 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
4088
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: li=;
4
4288
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 is stored in a double linked list, the whole list is sorted for the next round. My problem appears when subjecting the program to heavy load, that is, when I run the simulation for more than 10,000 miliseconds (every event occurs in...
5
6700
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 requirement of programs. If you don't know the difference between a list and array don't worry about it. A list is just an array without a name. They both can hold the same type of data: scalars or strings. I will use the terms "list" and "array" to...
1
7194
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 methods or syntax to a less experienced perl coder. I will post links to online resources you can read if necessary. Experienced perl coders might find nothing new or useful contained in this article. Short Review In part one I showed you some...
3
7349
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 uses unfamiliar terms and syntax. Intermediate and advanced Perl coders should find this article useful. The object of the article is to inform the reader, it is not about how to code Perl or how to write good Perl code, but to teach the Schwartzian...
0
9721
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9600
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9195
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7651
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6880
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5685
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3859
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.