Looking for a different version of sort

B

Brian McGonigle

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:
Traceback (most recent call last):

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
 
D

David Eppstein

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.
 
C

Cameron Laird

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?
 
B

Brian McGonigle

.
.
.
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top