How to sort a list? NOT a newbie question.
Tim Roberts
timr at probo.com
Wed Sep 19 07:54:06 CEST 2001
mjbarber at ascc.artsci.wustl.edu (Michael James Barber) wrote:
>All in Python 2.1.1:
>
>>>> l1 = [1, '1j', [1,'1j']]
>>>> l1.sort()
>>>> print l1
>[1, [1, '1j'], '1j']
>
>>>> l2 = [1, 1j, [1,'1j']]
>>>> l2.sort()
>Traceback (most recent call last):
> File "<input>", line 1, in ?
>TypeError: cannot compare complex numbers using <, <=, >, >=
>
>
>This came up in the context of building the coefficients of polynomials
>from a list of roots. If complex roots occur in complex conjugate pairs,
>then the resulting coefficients must be real, so I decided to cast the
>coefficients to floats in this case. A straightforward test is to take
>the complex conjugate of each element of the list, then sort both lists
>and see if they are the same. As is clear from the above, that definitely
>didn't work!
You can make this work if you can answer a couple of simple questions.
Which is greater: 3 or 3j?
Put these numbers in order: 3+0j 1+1j 1+2j 2+1j 2+2j 1+0j 0+3j
--
- Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.
More information about the Python-list
mailing list