By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,919 Members | 1,096 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,919 IT Pros & Developers. It's quick & easy.

Looking for a different version of sort

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.