简体   繁体   English

如何将二维复数数组与第一个的实部进行排序?

[英]How to sort a 2D array of complex numbers against the real part of the first?

I have a simple question about sorting. 我有一个关于排序的简单问题。 I have a 2D array with complex numbers like this one 我有一个像这样的复数二维数组

[[(-110.61040004379576-17.354473737234287j), (634.2701711342786+20.835585013392976j)],
 [(-56.513088619798417+19.304564206160592j), (139.27388305766863+22.035225424174627j)], 
 [(-56.107824445301581-15.926033069760162j), (139.59169287146287+21.499544204724476j)], 
 [(45.747228005094165-13.96425461096984j), (92.707153363055141-12.92524051712571j)], 
 [(-111.22348501455198-15.926033038872692j), (84.47603226469721+21.499544215863018j)], 
 [(45.747228023236914-13.964254617324347j), (92.707153338674217-12.925240518653737j)], 
 [(-56.107824379236632-15.926033025484159j), (139.59169284624946+21.499544241697734j)], 
 [(-111.62498538487328-18.53021375196538j), (138.66271112979905+18.818585903566003j)], 
 [(-56.107824363639025-15.926033025268273j), (139.59169284605719+21.499544250473367j)], 
 [(45.747228053266504-13.964254607367101j), (92.707153308435764-12.925240545154857j)]]

and I would like to sort it against the real part of the first complex number. 我想将其与第一个复数的实数部分进行排序。 I tried the .sort(key=lambda x: x[0].real) but it answers None. 我尝试了.sort(key=lambda x: x[0].real)但它回答了None。 Any bright ideas ? 有什么好主意吗? Thanks for helping. 感谢您的帮助。

R. R.

You've got None because sort method doesn't return anything, but sort list in-place. 您将获得None因为sort方法不会返回任何内容,而是就地排序列表。 So, if you want to create a separate sorted list, you can use sorted function, for example: sorted(l, key=lambda x: x[0].real) . 因此,如果要创建单独的排序列表,则可以使用排序函数,例如: sorted(l, key=lambda x: x[0].real) Or you can use your already sorted list after calling sort method. 或者,您可以在调用sort方法之后使用已经排序的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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