简体   繁体   中英

Python --> How to compare imaginary number in a list?

I am trying to understand how I could compare imaginary number in a list like so, and 1j is the imaginary number (-1 square root)

   x = [1, 1j]
   x.sort()
    Traceback (most recent call last):
      File "<pyshell#13>", line 1, in ?
        x.sort()
    TypeError: cannot compare complex numbers using <, <=, >, >=

You can use a bit of mathematics here rather than programming.

Complex numbers cannot be compared normally, but they have to be typically compared by comparing their modulus (sqrt of the sum of the squares of its real and complex parts). You can accordingly define functions for the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM