我想对2D数组进行排序以匹配另一个数组的排名
我已经尝试过sorted()
和sort()
函数,但是它不起作用。我不确定我的格式是否错误,或者执行此操作的最佳方法是什么。
array = (32,96)
ranked = [57, 23, 68, 58, 25, 91, 70, 83, 77, 75, 89, 34, 49, 79, 66, 54, 67,
44, 63, 52, 46, 20, 64, 10, 80, 33, 30, 29, 28, 26, 17, 27, 50, 51,
92, 86, 69, 47, 0, 7, 3, 85, 18, 11, 13, 53, 8, 78, 82, 81, 14,
74, 59, 32, 42, 39, 1, 31, 36, 19, 24, 5, 38, 9, 73, 71, 76, 87,
41, 55, 94, 93, 84, 16, 90, 62, 48, 43, 72, 95, 65, 45, 61, 22, 21,
15, 37, 88, 2, 40, 56, 6, 12, 60, 4, 35]
sortedarray = sorted(array,ranked)
对于上面的代码,我得到一个错误:
TypeError: 'list' object is not callable
排名为96的值与原始数组的第二维相对应。
答案 0 :(得分:1)
要对array
的第二维进行排序,您可以先制作一个形状为(32,96)的空sortedarray
,然后按ranked
指定的顺序填写新数组。这是使用array
的随机值的代码:
array = np.random.random((32,96))
ranked = [57, 23, 68, 58, 25, 91, 70, 83, 77, 75, 89, 34, 49, 79, 66, 54, 67,
44, 63, 52, 46, 20, 64, 10, 80, 33, 30, 29, 28, 26, 17, 27, 50, 51,
92, 86, 69, 47, 0, 7, 3, 85, 18, 11, 13, 53, 8, 78, 82, 81, 14,
74, 59, 32, 42, 39, 1, 31, 36, 19, 24, 5, 38, 9, 73, 71, 76, 87,
41, 55, 94, 93, 84, 16, 90, 62, 48, 43, 72, 95, 65, 45, 61, 22, 21,
15, 37, 88, 2, 40, 56, 6, 12, 60, 4, 35]
sortedarray = np.zeros(np.shape(array))
sortedarray[:, ranked] = array