468,733 Members | 1,204 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,733 developers. It's quick & easy.

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(lambda 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(list1,
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 1966
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(lambda 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(list1,
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(lambda 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(lambda 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(list1,
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.huwrites:
I then thought I'd just go events.sort(lambda x,y: x[2]<y[2]) and call
Use:
events.sort(lambda 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(lambda 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(list1,
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.sort)
Help on method_descriptor:

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.websiteburo.oops.comwrote:
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(lambda 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(list1,
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.sort)
Help on method_descriptor:

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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Philippe C. Martin | last post: by
7 posts views Thread by Foodbank | last post: by
1 post views Thread by xrow | last post: by
2 posts views Thread by Susan.Adkins | last post: by
19 posts views Thread by George Sakkis | last post: by
20 posts views Thread by martin-g | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.