473,473 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Looking for a different version of sort

I'm a Perl programmer learning Python (up to chapter 7 in Learning
Python, so go easy on me :-) and I find that I look to do things in
Python the way I would do them in Perl. In Perl functions and methods
usually only return and undefined value in the event of an error, make
an endless number of compound statements possible. Is there a version
of sort() I could import from somewhere that returns a reference to
the object on which it was performed, rather than returning "None".
t = ('x','y','z','a','b','c',)
t ('x', 'y', 'z', 'a', 'b', 'c') list(t) ['x', 'y', 'z', 'a', 'b', 'c'] l = list(t).sort()
print l None l = list(t)
l.sort()
l ['a', 'b', 'c', 'x', 'y', 'z']
I would like "list(t).sort()" to return a new sorted list. For
example, then I could do "t = tuple(list(t).sort())" to simulate an
in-place sort of a tuple assigned to the variable "t". When I try this
now I get:
t = tuple(list(t).sort()) Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: iteration over non-sequence


I assume this is because the "None" returned from sort() is the
non-sequence, but if sort had returned a reference to the list object
it was called upon, it would work.

Thanks,
Brian
Jul 18 '05 #1
3 1612
In article <8a**************************@posting.google.com >,
Bi*****@yahoo.com (Brian McGonigle) wrote:
I would like "list(t).sort()" to return a new sorted list. For
example, then I could do "t = tuple(list(t).sort())" to simulate an
in-place sort of a tuple assigned to the variable "t". When I try this
now I get:


There are situations when you might want to sort tuples, but they're
rare -- unless you need to use them as dict keys or something, it's more
likely that you should just be using lists.

But I think the actual answer to your question is that a sorted()
function is coming in Python 2.4 -- see e.g.
http://www.python.org/dev/doc/devel/whatsnew/node6.html
Once this is in place, you'd be able to do t = tuple(sorted(t))
without even turning it into a list first.

--
David Eppstein http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science
Jul 18 '05 #2
In article <8a**************************@posting.google.com >,
Brian McGonigle <Bi*****@yahoo.com> wrote:
I'm a Perl programmer learning Python (up to chapter 7 in Learning
Python, so go easy on me :-) and I find that I look to do things in
Python the way I would do them in Perl. In Perl functions and methods

Jul 18 '05 #3
cl****@lairds.com (Cameron Laird) wrote in message news:<10*************@corp.supernews.com>...
In article <8a**************************@posting.google.com >,
Brian McGonigle <Bi*****@yahoo.com> wrote:
I'm a Perl programmer learning Python (up to chapter 7 in Learning
Python, so go easy on me :-) and I find that I look to do things in
Python the way I would do them in Perl. In Perl functions and methods

.
.
.
Incidentally, is that the first or second edition you're reading?


First, thanks to all for the solutions. In another 40 pages I'll hit
the functions chapter and won't have to ask such newbie questions!

It's the second edition I'm reading, which covers Python 2.3. By the
way, what's the most common release? In Perl, I consider anything
older than 5.6 ancient and don't consider backwards compatability
beyond that point. Since programming is just a hobby, and I don't have
any customers, that's easy to do. For instance, I saw somewhere that
in 1.5 and prior, dir() wasn't available and you would have to use the
__method__ method or something similar to that. Should I worry about
stuff that old?
Jul 18 '05 #4

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

Similar topics

3
by: Jacinle Young | last post by:
Hi, I need to merge XML from different sources to 1 XML file. I am wondering if XSLT could do this job as usually it is used to perform 1 to 1 file transfrom eg. src1.xml <?xml...
8
by: Sebastian Kerekes | last post by:
Greetings, I'm developing an application that supports multiple languages. In my XSL I use variables to place the text where it belongs to. At the top of the document I include those variables -...
2
by: velthuijsen | last post by:
The STL sort only accepts a function that is defined in the form of bool Fname(<type>, <type>) in which <type> is the type of range to be sorted. I'm looking for a way to be able to sort the range...
2
by: Robert Wille | last post by:
I have found that the sort order is different on Windows and Linux (same version of Postgres). On Windows, it sorts in what appears to be normal ASCIIorder. On Linux, lowercase comes before...
6
by: Pedro Alves | last post by:
Hi I'm having serious problems with a mission critical app that runs on postgres (and has been running for the past 3 years). It's rather large, and lately things are not going well. The...
22
by: Marc Mones | last post by:
Hello, I'working with IBM DB2 V8.1 and CLI/ODBC. I've got a problem with the following statement: ******************************************************************************** SELECT...
1
by: Johann Blake | last post by:
I am looking for a good solution on how to implement data access in an application so that there is a clean separation between the data access layer, the business layer and the GUI layer. I am...
1
by: Chris H | last post by:
Im looking for a really easy to use and understand version of perls LWP function/module or whatver it is :-), Basically I am trying to fetch non RSS news from a couple websites that has news...
1
by: JpMaxMan | last post by:
Greetings. I'm having a strange problem on a Win 2003 server machine with VS 2005 installed. When I go to web site --asp.net configuration it loads the browswer window but instead of loading it...
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...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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 project—planning, coding, testing,...
0
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.