蟒蛇(numpy的)数组排序 [英] Python (Numpy) array sorting
问题描述
我有这个数组,命名为V,DTYPE的('float64')
I've got this array, named v, of dtype('float64'):
array([[ 9.33350000e+05, 8.75886500e+06, 3.45765000e+02],
[ 4.33350000e+05, 8.75886500e+06, 6.19200000e+00],
[ 1.33360000e+05, 8.75886500e+06, 6.76650000e+02]])
...我已经从文件中使用np.loadtxt命令收购。我想的第一列的值后排序它,没有,保持在同一行列在一起的数字结构混合起来。使用v.sort(轴= 0)给我:
... which I've acquired from a file by using the np.loadtxt command. I would like to sort it after the values of the first column, without mixing up the structure that keeps the numbers listed on the same line together. Using v.sort(axis=0) gives me:
array([[ 1.33360000e+05, 8.75886500e+06, 6.19200000e+00],
[ 4.33350000e+05, 8.75886500e+06, 3.45765000e+02],
[ 9.33350000e+05, 8.75886500e+06, 6.76650000e+02]])
...即放置在第一行第三列的最小数量等我宁愿希望这样的事情...
... i.e. places the smallest number of the third column in the first line, etc. I would rather want something like this...
array([[ 1.33360000e+05, 8.75886500e+06, 6.76650000e+02],
[ 4.33350000e+05, 8.75886500e+06, 6.19200000e+00],
[ 9.33350000e+05, 8.75886500e+06, 3.45765000e+02]])
...,其中每行中的元素没有被相对彼此移动。
... where the elements of each line hasn't been moved relatively to each other.
推荐答案
尝试
v[v[:,0].argsort()]
(用 v
作为数组)。 v [:0]
是第一列,而 .argsort()
返回指数将排序的第一个柱。然后,这个顺序适用于使用先进的索引整个阵列。请注意,您得到一个数组的sorte副本。
(with v
being the array). v[:,0]
is the first column, and .argsort()
returns the indices that would sort the first column. You then apply this ordering to the whole array using advanced indexing. Note that you get a sorte copy of the array.
我认识的到位数组排序的唯一方法是使用记录DTYPE:
The only way I know of to sort the array in place is to use a record dtype:
v.dtype = [("x", float), ("y", float), ("z", float)]
v.shape = v.size
v.sort(order="x")
这篇关于蟒蛇(numpy的)数组排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!