Re: Sorting a List of Lists



On Jan 31, 12:35 pm, Bruno Desthuilliers <bruno.
42.desthuilli...@xxxxxxxxxxxxxxxxxxxxxxxx> wrote:
apoth...@xxxxxxxxx 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/2007/01/28/23-python-sorting-
efficiency
for information on the relative speeds of rolling your own DSU versus
using itemgetter and key=...

- Paddy.


.



Relevant Pages

  • Re: Python 3.0 - is this true?
    ... items in the sort order, but it's important it doesn't fail to sort ... it may be that all the data items are lists of strings. ... of values retrieved from the database and asks Python to sort it. ...
    (comp.lang.python)
  • Newbie Python Refugee - Lexicographic Sorting of Nested Lists
    ... I'm trying to speed up my Scheme education by implementing some of my ... existing Python applications in Scheme. ... Tuples and lists were what ... I can sort the next level into ascending lexicographical order, ...
    (comp.lang.scheme)
  • Re: Python 3.0 - is this true?
    ... It heavily relies on being able to sort lists where ... appears to work in Python 2.x that's because you've been lucky to never ... need to sort objects that cause it to break. ... "compare anything" proxy class. ...
    (comp.lang.python)
  • Re: Python 3.0, rich comparisons and sorting order
    ... The user fills a column with arbitrary data, ... Presumably you could change how sort works so that it first stratifies ... is only defined for lists that take the form list. ... the vast majority of Python functions have type restrictions on their ...
    (comp.lang.python)
  • Re: Collections of non-arbitrary objects ?
    ... On Jun 26, 8:23 am, Bruno Desthuilliers <bruno. ... Whereas, with Python, the program will ... when I compare lists, to tuples, I notice that both are ordered ... Tuples have been compared to records/structures in other languages. ...
    (comp.lang.python)