472,330 Members | 1,503 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,330 software developers and data experts.

sorting a list of tupples

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"
Jul 18 '05 #1
4 4633

"David Bear" <da********@asu.edu> wrote in message
news:m3************@moroni.pp.asu.edu...
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

--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"

Jul 18 '05 #2
David Bear wrote:
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?
--
Daniel

Jul 18 '05 #3
On 30 Oct 2003 17:45:57 -0700, David Bear <da********@asu.edu> wrote:
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.
--
Steve Horne

steve at ninereeds dot fsnet dot co dot uk
Jul 18 '05 #4
John Roth wrote:

"David Bear" <da********@asu.edu> wrote in message
news:m3************@moroni.pp.asu.edu...
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.


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/li...q-mutable.html

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

Dave

--
Dave Kuhlman
http://www.rexx.com/~dkuhlman
dk******@rexx.com
Jul 18 '05 #5

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

Similar topics

21
by: Hilde Roth | last post by:
This may have been asked before but I can't find it. If I have a rectangular list of lists, say, l = ,,], is there a handy syntax for retrieving...
19
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am...
0
by: Brian Henry | last post by:
Here is another virtual mode example for the .NET 2.0 framework while working with the list view. Since you can not access the items collection of...
4
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...
20
by: martin-g | last post by:
Hi. Mostly I program in C++, and I'm not fluent in C# and .NET. In my last project I began to use LinkedList<and suddenly noticed that can't...
1
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....
3
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...
5
by: lemlimlee | last post by:
hello, this is the task i need to do: For this task, you are to develop a Java program that allows a user to search or sort an array of numbers...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...

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.