Re: sorting list of complex numbers

2008-11-21 Thread Tim Golden
Steven D'Aprano wrote: On Wed, 19 Nov 2008 18:39:27 -0800, Paul Rubin wrote: Terry Reedy <[EMAIL PROTECTED]> writes: Do your tuple destructuring in the first statement in your body and nothing will break. Why get rid of a useful feature that unclutters code? Unfortunately, the people who fi

Re: sorting list of complex numbers

2008-11-20 Thread Steven D'Aprano
On Wed, 19 Nov 2008 18:39:27 -0800, Paul Rubin wrote: > Terry Reedy <[EMAIL PROTECTED]> writes: >> >> Do your tuple destructuring in the first statement in your body and >> >> nothing will break. > > Why get rid of a useful feature that unclutters code? Unfortunately, the people who find it usef

Re: sorting list of complex numbers

2008-11-19 Thread Terry Reedy
Paul Rubin wrote: Terry Reedy <[EMAIL PROTECTED]> writes: Do your tuple destructuring in the first statement in your body and nothing will break. Why get rid of a useful feature that unclutters code? Because, as has been posted before, it is rarely used, it is replaceable, its presence inte

Re: sorting list of complex numbers

2008-11-19 Thread Paul Rubin
Terry Reedy <[EMAIL PROTECTED]> writes: > >> Do your tuple destructuring in the first statement in your body and > >> nothing will break. Why get rid of a useful feature that unclutters code? > > Unless you were using a lambda, which is quite useful as argument to > > "sort". > > So write a sepa

Re: sorting list of complex numbers

2008-11-19 Thread Hrvoje Niksic
Terry Reedy <[EMAIL PROTECTED]> writes: > Hrvoje Niksic wrote: >> Terry Reedy <[EMAIL PROTECTED]> writes: >> >>> Do your tuple destructuring in the first statement in your body and >>> nothing will break. >> >> Unless you were using a lambda, which is quite useful as argument to >> "sort". > > So

Re: sorting list of complex numbers

2008-11-19 Thread Terry Reedy
Hrvoje Niksic wrote: Terry Reedy <[EMAIL PROTECTED]> writes: Do your tuple destructuring in the first statement in your body and nothing will break. Unless you were using a lambda, which is quite useful as argument to "sort". So write a separate def statement. If you do not want to do that,

Re: sorting list of complex numbers

2008-11-19 Thread Hrvoje Niksic
Terry Reedy <[EMAIL PROTECTED]> writes: > Do your tuple destructuring in the first statement in your body and > nothing will break. Unless you were using a lambda, which is quite useful as argument to "sort". -- http://mail.python.org/mailman/listinfo/python-list

Re: sorting list of complex numbers

2008-11-18 Thread Terry Reedy
Paul Rubin wrote: That only goes so far though. Suppose instead of complex numbers you want to sort expressions, like: a(b,c(d,e),f(g,h)) treat those as parse trees in the obvious way. Presuming that a, c, and f are function calls that return Tree instances, you give Tree a __lt__ memb

Re: sorting list of complex numbers

2008-11-18 Thread Arnaud Delobelle
Paul Rubin writes: > for fancier structures you'd need a full blown class implementation > with an init method. Either way you end up temporarily allocating a > lot of extra structures, but at least they're not all in memory > simultaneously like in the DSU pattern. Th

Re: sorting list of complex numbers

2008-11-18 Thread Gabriel Genellina
En Tue, 18 Nov 2008 08:41:58 -0200, Paul Rubin <"http://phr.cx"@nospam.invalid> escribió: [EMAIL PROTECTED] writes: but how do I then do a secondary sort by the imaginary part,... Is there a way to do this using just the key arg, no extra data structures? Clever solutions involving multip

Re: sorting list of complex numbers

2008-11-18 Thread Paul Rubin
[EMAIL PROTECTED] writes: > but how do I then do a secondary sort by the imaginary part,... > Is there a way to do this using just the key arg, no extra data structures? Clever solutions involving multiple sorts aside, I think what they really want you to do is something like (untested): clas

Re: sorting list of complex numbers

2008-11-18 Thread Paul Rubin
Duncan Booth <[EMAIL PROTECTED]> writes: > If you don't like the tuple then just do the two sorts separately: > > >>> lst.sort(key=lambda x: x.imag) > >>> lst.sort(key=lambda x: x.real) > >>> pprint.pprint(lst) That only goes so far though. Suppose instead of complex numbers you want to sort exp

Re: sorting list of complex numbers

2008-11-11 Thread Steve Holden
Thomas Bellman wrote: > Steve Holden <[EMAIL PROTECTED]> wrote: > >> Only half the number, of course. The advantage of the key function is >> that each element requires only one call out to a Python function, and >> the comparisons then take place using a C-coded comparison function. > > You don'

Re: sorting list of complex numbers

2008-11-11 Thread Thomas Bellman
Steve Holden <[EMAIL PROTECTED]> wrote: > Only half the number, of course. The advantage of the key function is > that each element requires only one call out to a Python function, and > the comparisons then take place using a C-coded comparison function. You don't need any Python-coded function

Re: sorting list of complex numbers

2008-11-09 Thread skip
>> Timeit suggests the single sort returning the real, imag tuples is >> faster than two sorts each on one field, as you might expect since >> many fewer calls to the key function are made. Steve> Only half the number, of course. The advantage of the key Steve> function is tha

Re: sorting list of complex numbers

2008-11-09 Thread Steve Holden
[EMAIL PROTECTED] wrote: [...] > Duncan> If you don't like the tuple then just do the two sorts separately: > > lst.sort(key=lambda x: x.imag) > lst.sort(key=lambda x: x.real) > ... > > I tried that. I could have sworn when I read through the output it hadn't > retained

Re: sorting list of complex numbers

2008-11-09 Thread Terry Reedy
[EMAIL PROTECTED] wrote: Duncan> If you don't like the tuple then just do the two sorts separately: lst.sort(key=lambda x: x.imag) lst.sort(key=lambda x: x.real) ... I tried that. I could have sworn when I read through the output it hadn't retained the order of the r

Re: sorting list of complex numbers

2008-11-09 Thread Diez B. Roggisch
[EMAIL PROTECTED] schrieb: The thread on sorting in Python 3 got me to thinking. How could I sort a list of complex numbers using key? >>> lst = [random.random()+random.random()*1j for i in range(10)] >>> lst [(0.32672251849959244+0.41428983433288791j), (0.35238056484609881+0.92758

Re: sorting list of complex numbers

2008-11-09 Thread skip
>> Is there a way to do this using just the key arg, no extra data >> structures? Duncan> Is a tuple an extra data structure? lst.sort(key=lambda x: (x.real,x.imag)) pprint.pprint(lst) Duncan> [2.6001j, Duncan> 20j, Duncan> (1+2.73j), D

Re: sorting list of complex numbers

2008-11-09 Thread Duncan Booth
[EMAIL PROTECTED] wrote: > I can sort by the real parts just fine: > > >>> lst.sort(key=lambda x: x.real) > >>> pprint.pprint(lst) > [2.6001j, > 20j, > (1+2.73j), > (1+21j), > (2+2.8603j), > (2+22j), > (3+2.9902j), >

sorting list of complex numbers

2008-11-09 Thread skip
The thread on sorting in Python 3 got me to thinking. How could I sort a list of complex numbers using key? >>> lst = [random.random()+random.random()*1j for i in range(10)] >>> lst [(0.32672251849959244+0.41428983433288791j), (0.35238056484609881+0.92758203977208264j), (0.193378240