472,951 Members | 1,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

sorting a list of list

hi,

are there available library or pythonic algorithm for sorting a list
of list depending on the index of the list inside the list of my
choice?

d_list = [
['a', 1, 9],
['b', 2, 8],
['c', 3, 7],
['d', 4, 6],
['e', 5, 5],
]

Thanks
james
Nov 21 '07 #1
2 1468
are there available library or pythonic algorithm for sorting a list
of list depending on the index of the list inside the list of my
choice?
The built-in sorted() function and the sort() method on various
collections take an optional "key=function" keyword paramater
with which you can pass a function (lambdas are convenient) to
extract the bit on which you want to compare:
>>d_list = [
.... ['a', 1, 9],
.... ['b', 2, 8],
.... ['c', 3, 7],
.... ['d', 4, 6],
.... ['e', 5, 5],
.... ]
>>print sorted.__doc__
sorted(iterable, cmp=None, key=None, reverse=False) --new
sorted list
>>sorted(d_list, key=lambda x: x[2]) # sort by the 3rd item
[['e', 5, 5], ['d', 4, 6], ['c', 3, 7], ['b', 2, 8], ['a', 1, 9]]
>>sorted(d_list, key=lambda x: x[1]) # sort by the 2nd item
[['a', 1, 9], ['b', 2, 8], ['c', 3, 7], ['d', 4, 6], ['e', 5, 5]]
>>sorted(d_list, key=lambda x: x[0]) # sort by the 1st item
[['a', 1, 9], ['b', 2, 8], ['c', 3, 7], ['d', 4, 6], ['e', 5, 5]]

-tkc

Nov 21 '07 #2
Tim Chase wrote:
>are there available library or pythonic algorithm for sorting a list
of list depending on the index of the list inside the list of my
choice?

The built-in sorted() function and the sort() method on various
collections take an optional "key=function" keyword paramater
with which you can pass a function (lambdas are convenient) to
extract the bit on which you want to compare:
>>>d_list = [
... ['a', 1, 9],
... ['b', 2, 8],
... ['c', 3, 7],
... ['d', 4, 6],
... ['e', 5, 5],
... ]
>>>print sorted.__doc__
sorted(iterable, cmp=None, key=None, reverse=False) --new
sorted list
>>>sorted(d_list, key=lambda x: x[2]) # sort by the 3rd item
[['e', 5, 5], ['d', 4, 6], ['c', 3, 7], ['b', 2, 8], ['a', 1, 9]]
>>>sorted(d_list, key=lambda x: x[1]) # sort by the 2nd item
[['a', 1, 9], ['b', 2, 8], ['c', 3, 7], ['d', 4, 6], ['e', 5, 5]]
>>>sorted(d_list, key=lambda x: x[0]) # sort by the 1st item
[['a', 1, 9], ['b', 2, 8], ['c', 3, 7], ['d', 4, 6], ['e', 5, 5]]
Not to be too complicated, but there are functions that return a
callable that is faster than a lambda.
>>import operator
Then, instead of "lambda x: x[2]" use "operator.itemgetter(2)" and
instead of "lambda x: x.foo" use "operator.attrgetter('foo')".
--
Nov 21 '07 #3

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

Similar topics

20
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in Python, use the “sort” method. For example: ...
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...
4
by: slapsh0t11 | last post by:
Hello! I need help with a program that I believe I am nearly done with. However, there seems to be a few details that preclude me from success. Here is my assignment: Here is my class file...
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when 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.