numpy.sort_complex
numpy.sort_complex(a) [source]
首先使用实部,然后使用虚部对复杂数组进行排序。
参数 : | a :array_like 输入数组 |
返回值 : | out :complex ndarray 始终返回已排序的复杂数组。 |
例子
1)numpy.sort_complex
排序复数数组
import numpy as np
# 对实数数组进行复数排序(自动转换为复数)
a1 = [5, 3, 6, 2, 1]
sorted_a1 = np.sort_complex(a1)
print("排序后的结果(实数转复数):", sorted_a1)
# 输出: [1.+0.j 2.+0.j 3.+0.j 5.+0.j 6.+0.j]
# 对复数数组进行排序,优先按实部,其次按虚部排序
a2 = [1 + 2j, 2 - 1j, 3 - 2j, 3 - 3j, 3 + 5j]
sorted_a2 = np.sort_complex(a2)
print("排序后的结果(复数):", sorted_a2)
# 输出: [1.+2.j 2.-1.j 3.-3.j 3.-2.j 3.+5.j]
2)使用示例
import numpy as np
a = np.array([3 + 4j, 1 - 1j, 3 + 1j, 1 + 2j])
sorted_a = np.sort_complex(a)
print("原始数组:", a)
print("排序后:", sorted_a)