473,387 Members | 1,597 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,387 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 1486
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.