理想情况下 sort() 函数是多态性的极好例子。在 sort() 函数的情况下,您几乎可以用它对任何东西进行排序。
In [27]: b
Out[27]: [3, 4, 5, 6]
In [28]: b = ['a','b',5,6,None]
In [29]: b.sort()
In [30]: b
Out[30]: [None, 5, 6, 'a', 'b']
In [31]: b = ['a','b',23,'c',None,5j]
In [32]: b.sort()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/home/dubizzle/webapps/django/dubizzle/<ipython-input-32-fc40da74ac51> in <module>()
----> 1 b.sort()
TypeError: no ordering relation is defined for complex numbers
但似乎在虚数的情况下 sort() 函数失败。请注意,我收到此错误TypeError: no ordering relationship is defined for complex numbers。
所以我的问题是
- 这个顺序到底是在哪里定义的?sort() 函数如何在内部工作?
- 是否有任何目的为复数保留这种排序关系,或者它只是像我们在 c 语言中没有幂运算符一样被遗漏了(一个错误)。
- 我们如何对python中的虚数(复数)进行排序?我们有pythonic的方式来做到这一点吗?