【发布时间】:2019-03-04 13:03:33
【问题描述】:
我有一个结构化的 numpy 数组:
dtype = [('price', float), ('counter', int)]
values = [(35, 1), (36, 2),
(36, 3)]
a = np.array(values, dtype=dtype)
如果价格相等,我想按价格排序,然后按计数器排序:
a_sorted = np.sort(a, order=['price', 'counter'])[::-1]
我需要按降序排列的价格,当价格相等时,考虑按升序排列计数器。在上面的示例中,价格和计数器均按降序排列。
我得到的是:
a_sorted: [(36., 3), (36., 2), (35., 1)]
我需要的是:
a_sorted: [(36., 2), (36., 3), (35., 1)]
【问题讨论】:
标签: python arrays sorting numpy numpy-ndarray