473,696 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sorting a List of Lists

I can't seem to get this nailed down and I thought I'd toss it out
there as, by gosh, its got to be something simple I'm missing.

I have two different database tables of events that use different
schemas. I am using python to collate these records for display. I do
this by creating a list of lists that look roughly like this:

events = [['Event URL as String', 'Event Title as String ', Event Date
as Datetime], ...]

I then thought I'd just go events.sort(lam bda x,y: x[2]<y[2]) and call
it a day. That didn't work. But then lamda functions like to be very
simple, maybe object subscripts aren't allowed (even though I didn't
get an error). So I wrote a comparison function that looks much as you
would expect:

def date_compare(li st1,
list2):
x = list1[2]
y = list2[2]
if
x>y:
return
1
elif
x==y:
return
0
else: #
x<y
return -1

But as before sorting with this function returns None.

What have I overlooked?

Jan 31 '07 #1
7 2187
On Jan 30, 5:55 pm, apoth...@gmail. com wrote:
I can't seem to get this nailed down and I thought I'd toss it out
there as, by gosh, its got to be something simple I'm missing.

I have two different database tables of events that use different
schemas. I am using python to collate these records for display. I do
this by creating a list of lists that look roughly like this:

events = [['Event URL as String', 'Event Title as String ', Event Date
as Datetime], ...]

I then thought I'd just go events.sort(lam bda x,y: x[2]<y[2]) and call
it a day. That didn't work. But then lamda functions like to be very
simple, maybe object subscripts aren't allowed (even though I didn't
get an error). So I wrote a comparison function that looks much as you
would expect:

def date_compare(li st1,
list2):
x = list1[2]
y = list2[2]
if
x>y:
return
1
elif
x==y:
return
0
else: #
x<y
return -1

But as before sorting with this function returns None.

What have I overlooked?
All sorts return None. the sort is in place. Check your list post-
sort.

THN

Jan 31 '07 #2
events = [['Event URL as String', 'Event Title as String ', Event Date
as Datetime], ...]

I then thought I'd just go events.sort(lam bda x,y: x[2]<y[2]) and call
it a day. That didn't work. But then lamda functions like to be very
simple, maybe object subscripts aren't allowed (even though I didn't
get an error). So I wrote a comparison function that looks much as you
would expect:
Comparision functions must return -1, 0 or 1, not a bool.
You may use a key function instead in this case (requires python 2.4 or
newer):

events.sort(key =lambda x: x[2])

Viktor

Jan 31 '07 #3
ap******@gmail. com wrote:
I can't seem to get this nailed down and I thought I'd toss it out
there as, by gosh, its got to be something simple I'm missing.

I have two different database tables of events that use different
schemas. I am using python to collate these records for display. I do
this by creating a list of lists that look roughly like this:

events = [['Event URL as String', 'Event Title as String ', Event Date
as Datetime], ...]

I then thought I'd just go events.sort(lam bda x,y: x[2]<y[2]) and call
it a day. That didn't work. But then lamda functions like to be very
simple, maybe object subscripts aren't allowed (even though I didn't
get an error). So I wrote a comparison function that looks much as you
would expect:

def date_compare(li st1,
list2):
x = list1[2]
y = list2[2]
if
x>y:
return
1
elif
x==y:
return
0
else: #
x<y
return -1

But as before sorting with this function returns None.

What have I overlooked?
Sort doesn't return a list, it sorts in place. None is
the result code (if you will) of the sort completion.

-Larry
Jan 31 '07 #4
Létezo <le****@fw.huwr ites:
I then thought I'd just go events.sort(lam bda x,y: x[2]<y[2]) and call
Use:
events.sort(lam bda x,y: cmp(x[2], y[2]))
or:
events.sort(key =lambda x: x[2])
Jan 31 '07 #5
ap******@gmail. com a écrit :
I can't seem to get this nailed down and I thought I'd toss it out
there as, by gosh, its got to be something simple I'm missing.

I have two different database tables of events that use different
schemas. I am using python to collate these records for display. I do
this by creating a list of lists that look roughly like this:

events = [['Event URL as String', 'Event Title as String ', Event Date
as Datetime], ...]
Then you should not use a list of lists, but a list of tuples.
I then thought I'd just go events.sort(lam bda x,y: x[2]<y[2]) and call
it a day. That didn't work. But then lamda functions like to be very
simple, maybe object subscripts aren't allowed (even though I didn't
get an error). So I wrote a comparison function that looks much as you
would expect:

def date_compare(li st1,
list2):
x = list1[2]
y = list2[2]
if
x>y:
return
1
elif
x==y:
return
0
else: #
x<y
return -1

But as before sorting with this function returns None.

What have I overlooked?
Lol.

I guess this is a FAQ. list.sort() performs a destructive in-place sort,
and always return None. This is in the FineManual:

bruno@bruno:~$ python
Python 2.4.4c1 (#2, Oct 11 2006, 21:51:02)
[GCC 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu5)] on linux2
Type "help", "copyright" , "credits" or "license" for more information.
>>help(list.sor t)
Help on method_descript or:

sort(...)
L.sort(cmp=None , key=None, reverse=False) -- stable sort *IN PLACE*;
cmp(x, y) --1, 0, 1

You may want to use sorted(iterable , cmp=None, key=None, reverse=False)
if you don't want to sort in-place.

Also, using comparison functions is usually not the most efficient way
to do such a sort. In your case, I'd go for a good old
Decorate/sort/undecorate (AKA schwarzian transform):

events = [evt for date, evt in
sorted([(evt[2], evt) for evt in events])]
HTH
Jan 31 '07 #6
On Jan 31, 12:35 pm, Bruno Desthuilliers <bruno.
42.desthuilli.. .@wtf.websitebu ro.oops.comwrot e:
apoth...@gmail. com a écrit :
I can't seem to get this nailed down and I thought I'd toss it out
there as, by gosh, its got to be something simple I'm missing.
I have two different database tables of events that use different
schemas. I am using python to collate these records for display. I do
this by creating a list of lists that look roughly like this:
events = [['Event URL as String', 'Event Title as String ', Event Date
as Datetime], ...]

Then you should not use a list of lists, but a list of tuples.
I then thought I'd just go events.sort(lam bda x,y: x[2]<y[2]) and call
it a day. That didn't work. But then lamda functions like to be very
simple, maybe object subscripts aren't allowed (even though I didn't
get an error). So I wrote a comparison function that looks much as you
would expect:
def date_compare(li st1,
list2):
x = list1[2]
y = list2[2]
if
x>y:
return
1
elif
x==y:
return
0
else: #
x<y
return -1
But as before sorting with this function returns None.
What have I overlooked?

Lol.

I guess this is a FAQ. list.sort() performs a destructive in-place sort,
and always return None. This is in the FineManual:

bruno@bruno:~$ python
Python 2.4.4c1 (#2, Oct 11 2006, 21:51:02)
[GCC 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu5)] on linux2
Type "help", "copyright" , "credits" or "license" for more information.
>>help(list.sor t)
Help on method_descript or:

sort(...)
L.sort(cmp=None , key=None, reverse=False) -- stable sort *IN PLACE*;
cmp(x, y) --1, 0, 1

You may want to use sorted(iterable , cmp=None, key=None, reverse=False)
if you don't want to sort in-place.

Also, using comparison functions is usually not the most efficient way
to do such a sort. In your case, I'd go for a good old
Decorate/sort/undecorate (AKA schwarzian transform):

events = [evt for date, evt in
sorted([(evt[2], evt) for evt in events])]

HTH
I agree with you B., but see the comments here:
http://www.biais.org/blog/index.php/...ython-sorting-
efficiency
for information on the relative speeds of rolling your own DSU versus
using itemgetter and key=...

- Paddy.
Jan 31 '07 #7
Paddy a écrit :
On Jan 31, 12:35 pm, Bruno Desthuilliers <bruno.
(snip)
>>Also, using comparison functions is usually not the most efficient way
to do such a sort. In your case, I'd go for a good old
Decorate/sort/undecorate (AKA schwarzian transform):

events = [evt for date, evt in
sorted([(evt[2], evt) for evt in events])]

HTH


I agree with you B., but see the comments here:
http://www.biais.org/blog/index.php/...ython-sorting-
efficiency
for information on the relative speeds of rolling your own DSU versus
using itemgetter and key=...
Yeps, looks like 2.5 got a real speedup wrt/ itemgetter. Nice to know,
and thanks for the link (BTW, this profileit() decorator looks pretty
nice too !-)

Jan 31 '07 #8

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

Similar topics

10
2174
by: Philippe C. Martin | last post by:
Hi, I'm looking for an easy algorithm - maybe Python can help: I start with X lists which intial sort is based on list #1. I want to reverse sort list #1 and have all other lists sorted accordingly. Any idea is welcome.
2
8249
by: Kakarot | last post by:
I'm gona be very honest here, I suck at programming, *especially* at C++. It's funny because I actually like the idea of programming ... normally what I like I'm atleast decent at. But C++ is a different story. Linked lists have been giving me a headache. I can barely manage to create a single/doubly linked list, it's just that when it gets to merge sorting or searching (by reading shit data from text files), I lose track.
7
10081
by: Foodbank | last post by:
Hi everyone. I'm having trouble with this radix sorting program. I've gotten some of it coded except for the actual sorting :( The book I'm teaching myself with (Data Structures Using C and C++) just doesn't explain things good at all and the tutorials I've viewed don't really explain least significant digit first sorting or show examples, which would be most helpful. I've commented the section where I know that the iteration of the...
1
9495
by: xrow | last post by:
Hello I have a simple webservice / c# application that receives data from server and prints the data in the asp:datagrid control I have problem when sorting data in datagrid I have created SortCommand event in my .aspx file for my datagrid DataGrid2 but I am not able to access my datarecord nor dataset from my SortCommand event. I recieve also an error when running with created event if don't change event method declaration to public void...
4
4281
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 is stored in a double linked list, the whole list is sorted for the next round. My problem appears when subjecting the program to heavy load, that is, when I run the simulation for more than 10,000 miliseconds (every event occurs in...
2
6987
by: Susan.Adkins | last post by:
Alright, I must say that the problem my teacher has given us to do has royally annoyed me. We are to take a letter *txt file* and then read in the words. Make 2 seperate lists *even and odd* for the number of occurances... so the first time a word is entered its going to have an occurance of one and be in the odd list then it is going to go to the next word see if its in the odd list. If its not its going to check the even list. if...
19
2900
by: George Sakkis | last post by:
It would be useful if list.sort() accepted two more optional parameters, start and stop, so that you can sort a slice in place. In other words, x = range(1000000) x.sort(start=3, stop=-1) would be equivalent to x = sorted(x)
3
2387
by: acosgaya | last post by:
Hi, I would like some help as how to approach the following problem: I have a set of d-dimensional points (e.g (3,5,8,9,5) is a 5-dimensional point), and I need to sort the points on each of the coordinates. The algorithm that I am trying to implement says that the resulting data structure is a multi-pointer list ((d-1)tuply threaded list), each node of which is threaded in each of the single pointer lists corresponding to the chosen...
20
20905
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 sorting for LinkedList<myself? Thanks in advance Martin
0
8661
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
8595
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
9142
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
9008
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
6512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4608
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3030
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
2313
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1989
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.