473,396 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 4694

"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 the ith item of every sublist? I know about for i...
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 confused as to how I would provide functionality to...
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 the list view you need to do sorting another...
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 certain amount of time. Every time that an event...
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 find a way to sort it. Does it mean I must implement...
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. Some of the techniques might introduce unfamiliar...
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 article. Beginning Perl coders may find this article...
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 using an algorithm that the user chooses. The...
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 remove code that I think controls how many colums...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.