sorting list of complex numbers

Steve Holden steve at holdenweb.com
Tue Nov 11 21:19:31 EST 2008


Thomas Bellman wrote:
> Steve Holden <steve at holdenweb.com> 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 at all.  The operator
> module is your friend: key=operator.attrgetter('real', 'imag')
> will create the required tuples for sorting.
> 
True; "requires only one call out to the key function", then. You're
right, attrgetter will be faster still, and it's a really neat solution.

regards
 Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/




More information about the Python-list mailing list