我有一个numpy
结构数组
import numpy as np
np.array([(0, 1, 1167606000), (0, 1, 1167606005), (0, 1, 1167606008),
(0, 10, 1167606010), (0, 10, 1167606012), (1, 0, 1167606000),
(1, 2, 1167606001), (1, 0, 1167606005), (1, 0, 1167606008),
(2, 1, 1167606001), (2, 3, 1167606002), (3, 2, 1167606002),
(3, 4, 1167606003), (4, 3, 1167606003), (4, 5, 1167606004),
(5, 4, 1167606004), (5, 6, 1167606005), (6, 5, 1167606005),
(6, 7, 1167606006), (7, 6, 1167606006), (7, 8, 1167606007),
(8, 7, 1167606007), (8, 9, 1167606008), (9, 8, 1167606008),
(9, 10, 1167606009), (10, 9, 1167606009), (10, 0, 1167606010),
(10, 0, 1167606012)],
dtype=[('fr', '<i8'), ('to', '<i8'), ('time', '<i8')])
是否有矢量化方式首先按“fr”,“to”和“time”的最小值对其进行排序。另一件事是,我希望sort
没有制作任何副本。
修改 排序不是'fr','to'和'time',而是首先是'fr'和'to'的最小值,然后是'time'。上述案例中的预期答案是
(0, 1, 1167606000),
(1, 0, 1167606000),
(0, 1, 1167606005),
(1, 0, 1167606005),
(0, 1, 1167606008),
(1, 0, 1167606008),
(0, 10, 1167606010),
(0, 10, 1167606012),
(1, 2, 1167606001),
(2, 1, 1167606001),
(2, 3, 1167606002),
(3, 2, 1167606002),
(3, 4, 1167606003),
(4, 3, 1167606003),
...
答案 0 :(得分:2)
您可以向order
提供sort
参数:
a.sort(order=['fr', 'to', 'time'])
使用lexsort
,您可以按任意键排序。在这里,给它a['time']
和np.minimum(a['to'], a['fr'])
(按最后一项排序)
inds = np.lexsort((a['time'], np.minimum(a['to'], a['fr'])))
a = a[inds]
为避免在重新排列时复制a
,您可以使用take
代替a = a[inds]
np.take(a, inds, out=a)