在Python中相对于另一个数组的一个数组的更改顺序

发布于 2025-02-11 01:08:18 字数 618 浏览 1 评论 0原文

我有两个数组ab,一对一的信件IE值与[0,1]中的值10在b中,中的中的 中的等等。现在,我将a排序以产生新的数组a1。如何对A1生成b1?所需的输出已连接。

import numpy as np
A = np.array([[[0, 1],[0, 2],[1, 3],[2, 3],[2, 5],[3, 4],[3, 6],[4, 7],[5, 6],[6, 7]]])
B = np.array([[[10],[20],[30],[40],[50],[60],[70],[80],[90],[100]]])
A1=np.sort(A,axis=1)
#A1=array([[[0, 1],[0, 2],[1, 3],[2, 3],[3,4],[2, 5],[3, 6],[5, 6],[4, 7],[6, 7]]])

所需的输出是

B1=array([[[10],[20],[30],[40],[60],[50],[70],[90],[80],[100]]])

I have two arrays A and B with one-to-one correspondence i.e. values corresponding to [0,1] in A is 10 in B, [0,2] in A is 20 in B and so on. Now I sort A to yield a new array A1. How do I generate B1 with respect to A1? The desired output is attached.

import numpy as np
A = np.array([[[0, 1],[0, 2],[1, 3],[2, 3],[2, 5],[3, 4],[3, 6],[4, 7],[5, 6],[6, 7]]])
B = np.array([[[10],[20],[30],[40],[50],[60],[70],[80],[90],[100]]])
A1=np.sort(A,axis=1)
#A1=array([[[0, 1],[0, 2],[1, 3],[2, 3],[3,4],[2, 5],[3, 6],[5, 6],[4, 7],[6, 7]]])

The desired output is

B1=array([[[10],[20],[30],[40],[60],[50],[70],[90],[80],[100]]])

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

擦肩而过的背影 2025-02-18 01:08:20

您可以使用argsort()函数和[:]运算符来刺激所需的结果:

A = np.array([[[0, 1],[0, 2],[1, 3],[2, 5],[3, 4],[4, 7],[5, 6]]])
order = A[0,:, 1].argsort()
A =np.array([B[0][order]])
print(A)

You can use argsort() function and [:] operator to ahieve the desired result:

A = np.array([[[0, 1],[0, 2],[1, 3],[2, 5],[3, 4],[4, 7],[5, 6]]])
order = A[0,:, 1].argsort()
A =np.array([B[0][order]])
print(A)
柒七 2025-02-18 01:08:20

您应该使用argsort

导入numpy作为NP

A = np.array([[[0, 1],[0, 2],[1, 3],[2, 3],[2, 5],[3, 4],[3, 6],[4, 7],[5, 6],[6, 7]]])
B = np.array([[[10],[20],[30],[40],[50],[60],[70],[80],[90],[100]]])
idxs = np.argsort(A, axis=1)

A1 = np.take_along_axis(A, idxs, axis=1)
B1 = np.take_along_axis(B, idxs, axis=1)

You should use argsort:

import numpy as np

A = np.array([[[0, 1],[0, 2],[1, 3],[2, 3],[2, 5],[3, 4],[3, 6],[4, 7],[5, 6],[6, 7]]])
B = np.array([[[10],[20],[30],[40],[50],[60],[70],[80],[90],[100]]])
idxs = np.argsort(A, axis=1)

A1 = np.take_along_axis(A, idxs, axis=1)
B1 = np.take_along_axis(B, idxs, axis=1)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文