下面是一个互动的环节,测试我的评论中的想法:In [1]: import numpy as np
In [2]: dt = np.dtype([('destID',int),('ATTRACT',float),('other','S10')])
In [3]: TableArr=np.zeros((10,),dt)
In [5]: TableArr['destID']=np.random.randint(10,size=(10,))
In [6]: TableArr['ATTRACT']=np.random.randint(100,size=(10,))
In [7]: TableArr
Out[7]:
array([(2, 39.0, b''), (7, 7.0, b''), (8, 74.0, b''), (5, 83.0, b''),
(5, 3.0, b''), (9, 26.0, b''), (8, 9.0, b''), (3, 1.0, b''),
(1, 67.0, b''), (7, 5.0, b'')],
dtype=[('destID', '
In [13]: Tcopy=TableArr[['destID','ATTRACT']].copy()
# use copy() to avoid a FutureWarning
In [14]: Tcopy['ATTRACT'] *= -1 # 'reverse' a field
In [16]: I=np.argsort(Tcopy,order=['destID','ATTRACT'])
In [17]: I
Out[17]: array([8, 0, 7, 3, 4, 1, 9, 2, 6, 5], dtype=int32)
In [18]: TableArr[I]
Out[18]:
array([(1, 67.0, b''), (2, 39.0, b''), (3, 1.0, b''), (5, 83.0, b''),
(5, 3.0, b''), (7, 7.0, b''), (7, 5.0, b''), (8, 74.0, b''),
(8, 9.0, b''), (9, 26.0, b'')],
dtype=[('destID', '
整数在增加,在三种情况下,它们联系在一起,浮点数在减少。所以它起作用了。在