对复数列表进行排序并获取排序索引
Sort list of complex numbers and get sorting indexes
我需要按虚部对 list
个复数进行排序。我找到了问题“sorting list of complex numbers”的解决方案。
只需使用带有适当键的排序命令:
list_ordered = sorted(list, key=lambda x: x.imag)
我还想取回排序索引。 Another existing solution 不适用于复数。在我的案例中是否有提取索引的优雅解决方案?
谢谢!
second answer to the question you linked可以很容易地适应:
complexes = [1, 2+3j, 1-2j, 6+1j]
[i[0] for i in sorted(enumerate(complexes), key=lambda x:x[1].imag)]
# [2, 0, 3, 1]
我需要按虚部对 list
个复数进行排序。我找到了问题“sorting list of complex numbers”的解决方案。
只需使用带有适当键的排序命令:
list_ordered = sorted(list, key=lambda x: x.imag)
我还想取回排序索引。 Another existing solution 不适用于复数。在我的案例中是否有提取索引的优雅解决方案?
谢谢!
second answer to the question you linked可以很容易地适应:
complexes = [1, 2+3j, 1-2j, 6+1j]
[i[0] for i in sorted(enumerate(complexes), key=lambda x:x[1].imag)]
# [2, 0, 3, 1]