Python - >如何比较列表中的虚数?

时间:2014-07-22 17:28:53

标签: python

我试图理解如何比较像这样的列表中的虚数,1j是虚数(-1平方根)

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

1 个答案:

答案 0 :(得分:1)

你可以在这里使用一些数学而不是编程。

复数不能正常比较,但必须通过比较它们的模数(其实部和复杂部分的平方和的sqrt)进行比较。因此,您可以定义相同的函数。