Re: Multiple "cmp"s chained one after another

2005-05-16 Thread Glauco Silva
You can try this : >>> l = [(2001, 5, 2),(2111,3,3),(1984, 3, 1), (2001, 1, 1)] >>> l.sort(lambda x = l[0],y = l[1] : cmp((x[1],x[2]),(y[1],y[2]))) - Original Message - From: "Volker Grabsch" <[EMAIL PROTECTED]> To: Sent: Saturday, May 14, 2005 7:09 A

Re: Multiple "cmp"s chained one after another

2005-05-14 Thread Peter Hansen
Volker Grabsch wrote: > Peter Hansen wrote: > >>Or just use the .timetuple() method on datetime objects and sort on the >>8th element of the 9-element tuple, which is the day-of-the-year. > > An interesting idea. But wouldn't sorting by (dd.month,dd.day) be more > effective? Depending on the me

Re: Multiple "cmp"s chained one after another

2005-05-14 Thread Volker Grabsch
vincent wehren wrote: > >| > If you don't care about the year, why not just "normalize" the year >| > to all be the same using the replace method of the date instance? >| >| That's a very bad idea. In my example, this would work, but in "reality" >| I don't sort datetime objects, of course! (Is th

Re: Multiple "cmp"s chained one after another

2005-05-14 Thread vincent wehren
"Volker Grabsch" <[EMAIL PROTECTED]> schrieb im Newsbeitrag news:[EMAIL PROTECTED] | vincent wehren wrote: | > | > If you don't care about the year, why not just "normalize" the year | > to all be the same using the replace method of the date instance? | | That's a very bad idea. In my example, t

Re: Multiple "cmp"s chained one after another

2005-05-14 Thread Volker Grabsch
Peter Hansen wrote: > > Or just use the .timetuple() method on datetime objects and sort on the > 8th element of the 9-element tuple, which is the day-of-the-year. An interesting idea. But wouldn't sorting by (dd.month,dd.day) be more effective? In other words: Does .timetuple() create a tuple,

Re: Multiple "cmp"s chained one after another

2005-05-14 Thread Volker Grabsch
vincent wehren wrote: > > If you don't care about the year, why not just "normalize" the year > to all be the same using the replace method of the date instance? That's a very bad idea. In my example, this would work, but in "reality" I don't sort datetime objects, of course! (Is there any real a

Re: Multiple "cmp"s chained one after another

2005-05-14 Thread Volker Grabsch
Steven Bethard wrote: > Robert Kern wrote: > def mykey(d): > return (d.month, d.day) > > The point here is that rather than chaining cmp() calls with ors, you > should just use a tuple -- the standard comparison order in tuples is > exactly what you're looking for. That's an excel

Re: Multiple "cmp"s chained one after another

2005-05-14 Thread Steven Bethard
Robert Kern wrote: > I find that using the "key" argument to sort is much nicer than "cmp" > for these tasks. > > In [5]:L = [datetime.date(2005,5,2), datetime.date(1984,12,15), > datetime.date(1954,1,1)] > > In [7]:L.sort(key=lambda x: (x.month, x.day)) > > In [8]:L > Out[8]: > [datetime.date

Re: Multiple "cmp"s chained one after another

2005-05-14 Thread Peter Hansen
vincent wehren wrote: > "Volker Grabsch" <[EMAIL PROTECTED]> schrieb im > Newsbeitrag news:[EMAIL PROTECTED] > | However, I don't want to sort them the default way. These are birthdays, > | so only the month and day do matter, not the year. E.g.: > | ... > If you don't care about the year, why not

Re: Multiple "cmp"s chained one after another

2005-05-14 Thread Robert Kern
Volker Grabsch wrote: > Hello! > > Ich just found a very nice 'pythonic' solution for an often appearing > problem. I didn't find it documented anywhere, so I'm posting it here. > If this isn't new in any way, I'd really like to get to know it. > > Example problem: > I have some "datetime" object

Re: Multiple "cmp"s chained one after another

2005-05-14 Thread vincent wehren
"Volker Grabsch" <[EMAIL PROTECTED]> schrieb im Newsbeitrag news:[EMAIL PROTECTED] | Hello! | | Ich just found a very nice 'pythonic' solution for an often appearing | problem. I didn't find it documented anywhere, so I'm posting it here. | If this isn't new in any way, I'd really like to get to

Multiple "cmp"s chained one after another

2005-05-14 Thread Volker Grabsch
Hello! Ich just found a very nice 'pythonic' solution for an often appearing problem. I didn't find it documented anywhere, so I'm posting it here. If this isn't new in any way, I'd really like to get to know it. Example problem: I have some "datetime" objects and want to sort them, as here: