如何根据其中一个值有效地对两个数组进行分类?
如果我有两个数组:
a = [ 1, 4, 3, 2]
b = ['z', 'w', 'x', 'y']
值对 a
a b 进行排序。
[ 1, 2, 3, 4] # a
['z','y','x','w'] # b
并且我想根据 a
的 惯用方式做到这一点?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于两个不同的向量,我不知道这是否可能。
我将使用 sortperm 返回一个返回置换式vector
> p
将a [p]
放置顺序排序的顺序,如果性能很重要,您还可以使用
sortperm!
,允许使用preallocated置换置换量向量p
。注意:如果您有矩阵,则可以使用分类
With two distinct vectors I do not know if this is possible.
I would use sortperm that returns a permutation vector
p
that putsa[p]
in sorted orderIf performance is important you can also use
sortperm!
that allows using a preallocated permutation vectorp
.Note: if you have a matrix you can use sortslices