在Python中排序索引
我有一个数组i1
。我想按增加j
顺序排列索引。例如,在[0,1]
,i = 0,j = 1
中。但是我有一个错误。预期的输出已附加。
import numpy as np
I1=np.array([[[0, 1],
[0, 3],
[1, 2],
[1, 4],
[2, 5],
[3, 4],
[3, 6],
[4, 7],
[5, 4],
[6, 7]],
[[0, 1],
[0, 3],
[1, 2],
[1, 4],
[2, 5],
[3, 4],
[3, 6],
[4, 7]]])
order1 = I1[0,:, 1].argsort()
I10 =np.array([I1[0][order1]])
print("I10 =",[I10])
错误是
in <module>
order1 = I1[0,:, 1].argsort()
IndexError: too many indices for array: array is 1-dimensional, but 3 were indexed
预期的输出是:
array([[[0, 1],
[1, 2],
[0, 3],
[1, 4],
[3, 4],
[5, 4],
[2, 5],
[3, 6],
[4, 7],
[6, 7]],
[[0, 1],
[1, 2],
[0, 3],
[1, 4],
[3, 4],
[2, 5],
[3, 6],
[4, 7]]])
I have an array I1
. I want to arrange the indices in order of increasing j
. For example, in [0, 1]
, i=0,j=1
. But I am getting an error. The expected output is attached.
import numpy as np
I1=np.array([[[0, 1],
[0, 3],
[1, 2],
[1, 4],
[2, 5],
[3, 4],
[3, 6],
[4, 7],
[5, 4],
[6, 7]],
[[0, 1],
[0, 3],
[1, 2],
[1, 4],
[2, 5],
[3, 4],
[3, 6],
[4, 7]]])
order1 = I1[0,:, 1].argsort()
I10 =np.array([I1[0][order1]])
print("I10 =",[I10])
The error is
in <module>
order1 = I1[0,:, 1].argsort()
IndexError: too many indices for array: array is 1-dimensional, but 3 were indexed
The expected output is:
array([[[0, 1],
[1, 2],
[0, 3],
[1, 4],
[3, 4],
[5, 4],
[2, 5],
[3, 6],
[4, 7],
[6, 7]],
[[0, 1],
[1, 2],
[0, 3],
[1, 4],
[3, 4],
[2, 5],
[3, 6],
[4, 7]]])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用
键
sorted()
python内置功能:输出:
You need to use the
key
parameter ofsorted()
Python built-in function:Output: