473,657 Members | 2,582 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sorting list of tuples by second (third...) tuple item

Hi,

I'm not in the mailing list.
By Googling, I stepped into this an old post: (Thu Feb 14 20:40:08 CET 2002)
of Jeff Shannon:
http://mail.python.org/pipermail/pyt...ry/128438.html

<<<
def SortOnItem(myli st, index):
templist = [ (line[index], line) for line in mylist ]
templist.sort()
return [ line[1:] for line in templist ]

What this does is build a separate list containing a tuple of the
element that you want to sort on, and the entire line, sorts that
list (by the first element, of course), and then strips that first
element off ..
>>>
It seems to me that the tuples aren't sorted only by the first element but,
I suppose, other elements are also used if needed to discriminate.
In some cases I got some exceptions when an element of the tuple, other than
the first, didn't admit comparison.
In these cases I had to use an ad hoc comparison function.

Regards, Giovanni Toffoli

Feb 16 '07 #1
1 2870
Giovanni Toffoli a écrit :
Hi,

I'm not in the mailing list.
By Googling, I stepped into this an old post: (Thu Feb 14 20:40:08 CET
2002) of Jeff Shannon:
http://mail.python.org/pipermail/pyt...ry/128438.html

<<<
def SortOnItem(myli st, index):
templist = [ (line[index], line) for line in mylist ]
templist.sort()
return [ line[1:] for line in templist ]

What this does is build a separate list containing a tuple of the
element that you want to sort on, and the entire line, sorts that
list (by the first element, of course), and then strips that first
element off ..
It's the "decorate/sort/undecorate" pattern. You may also google for
"schwarzian transform"
>>>>

It seems to me that the tuples aren't sorted only by the first element
but, I suppose, other elements are also used if needed to discriminate.
Yes. When compared, tuples first compare on the first element, then on
the second etc...
In some cases I got some exceptions when an element of the tuple, other
than the first, didn't admit comparison.
In these cases I had to use an ad hoc comparison function.
Did you try the sorted() function ? Used with operator.itemge tter as the
'key' argument, it may do the trick.
Feb 16 '07 #2

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

Similar topics

39
6046
by: Erlend Fuglum | last post by:
Hi everyone, I'm having some trouble sorting lists. I suspect this might have something to do with locale settings and/or character encoding/unicode. Consider the following example, text containing norwegian special characters æ, ø and å. >>> liste =
4
4708
by: David Bear | last post by:
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
8
2765
by: Nickolay Kolev | last post by:
Hi all, I have a list whose length is a multiple of 3. I want to get a list of tuples each of which has 3 consecutive elements from the original list, thus packing the list into smaller packets. Like this: l = tups =
66
4976
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
14
12272
by: Richard | last post by:
I have a large list of two element tuples. I want two separate lists: One list with the first element of every tuple, and the second list with the second element of every tuple. Each tuple contains a datetime object followed by an integer. Here is a small sample of the original list: ((datetime.datetime(2005, 7, 13, 16, 0, 54), 315), (datetime.datetime(2005, 7, 13, 16, 6, 12), 313),
8
2259
by: nidhog | last post by:
Hello guys, I made a script that extracts strings from a binary file. It works. My next problem is sorting those strings. Output is like: ---- snip ---- 200501221530
17
2785
by: John Salerno | last post by:
Hi everyone. If I have a list of tuples, and each tuple is in the form: (year, text) as in ('1995', 'This is a citation.') How can I sort the list so that they are in chronological order based on the year? Is there a better way to do this than making a list of tuples? (So far I have a text file and on each line is a citation. I use an RE to search for the year, then put this year and the entire citation in a tuple, and add this tuple...
10
5144
by: rshepard | last post by:
While working with lists of tuples is probably very common, none of my five Python books or a Google search tell me how to refer to specific items in each tuple. I find references to sorting a list of tuples, but not extracting tuples based on their content. In my case, I have a list of 9 tuples. Each tuple has 30 items. The first two items are 3-character strings, the remaining 28 itmes are floats. I want to create a new list from...
16
9856
by: skip | last post by:
The thread on sorting in Python 3 got me to thinking. How could I sort a list of complex numbers using key? As expected: Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: no ordering relation is defined for complex numbers
0
8392
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8305
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8825
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1953
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.