sorting a list of tupples

D

David Bear

I have a list of tupples. Each tupple has 3 items. The first item is
an integer. I'd like to sort the list(of tupples), based on the first
element of the tupple. I thought about stringifying the tupples, then sorting
those, but thought there must be a better way. any idea's?

--David Bear
phone: 480-965-8257
fax: 480-965-9189
College of Public Programs/ASU
Wilson Hall 232
Tempe, AZ 85287-0803
"Beware the IP portfolio, everyone will be suspect of trespassing"
 
J

John Roth

David Bear said:
I have a list of tupples. Each tupple has 3 items. The first item is
an integer. I'd like to sort the list(of tupples), based on the first
element of the tupple. I thought about stringifying the tupples, then sorting
those, but thought there must be a better way. any idea's?

<list>.sort() should do the job, as long as you want the result
to be a list in ascending order by the value of the first element
of the tuple.

John Roth
 
D

Daniel Luz

David said:
I have a list of tupples. Each tupple has 3 items. The first item is
an integer. I'd like to sort the list(of tupples), based on the first
element of the tupple. I thought about stringifying the tupples, then sorting
those, but thought there must be a better way. any idea's?

Have you tried just sort()ing the list? Tuples naturally will sort based
on the first element of them (then, if two elements are equal, it will
try to sort based on the second element, and so forth).

>>> l = [(1, 'p'), (6, 'n'), (4, 'h'), (3, 't'), (5, 'o'), (2, 'y')]
>>> l.sort()
>>> l
[(1, 'p'), (2, 'y'), (3, 't'), (4, 'h'), (5, 'o'), (6, 'n')]


If that's not what you meant, could you please clarify?
 
S

Stephen Horne

I have a list of tupples. Each tupple has 3 items. The first item is
an integer. I'd like to sort the list(of tupples), based on the first
element of the tupple. I thought about stringifying the tupples, then sorting
those, but thought there must be a better way. any idea's?

You can sort a list of tuples using the standard sort method. Ordering
of tuples is derived much like ordering of strings - it will use the
order of the first items in the tuples unless they are equal in which
case it uses the second items, or third items, or whatever. If all
else fails, the longest tuple is considered greater.

If you are sorting on the first item, the sort method should just work
for you.
 
D

Dave Kuhlman

John said:
<list>.sort() should do the job, as long as you want the result
to be a list in ascending order by the value of the first element
of the tuple.

And, if you don't want to sort on the first element or want to
sort in descending order or ... you will want to know about the
optional argument to the sort method. You can pass it your own
custom comparison function. Read about it here:

http://www.python.org/doc/current/lib/typesseq-mutable.html

The info you (might) want is down in a footnote.

Dave
 

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

Similar Threads

tar interface 4
mod_python and zope 1
win32 file attributes 2
testing the data-type of a stream 1
confused about stdin 1
regular expression in strings 1
getopt issues 6
Sorting a List of Lists 7

Members online

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top