Python 索引排序表
如何根据索引在python中指向的内容对索引列表进行排序 我有Python 索引排序表,python,sorting,Python,Sorting,如何根据索引在python中指向的内容对索引列表进行排序 我有 indices = list(range(len(mylist))) 我想对索引进行排序,这样如果索引中a在b之前,那么mylist[a]>mylist[b]另外,让我使用原始问题中提供的信息添加以下片段作为答案 indicies.sort(lambda x,y:mylist[x]-mylist[y]) 我想对索引进行排序,以便 在索引中a在b之前,然后是mylist[a]>mylist[b] …以及您在评论中提供的其他信息
indices = list(range(len(mylist)))
我想对索引进行排序,这样如果索引中
a
在b
之前,那么mylist[a]>mylist[b]
另外,让我使用原始问题中提供的信息添加以下片段作为答案
indicies.sort(lambda x,y:mylist[x]-mylist[y])
我想对索引进行排序,以便
在索引中a在b之前,然后是mylist[a]>mylist[b]
…以及您在评论中提供的其他信息
另外,如果mylist是一个 numpy浮动 然后你可以简单地做:
In [2]: import numpy
In [3]: a = numpy.asarray([-1, 2.73, 15.827, -8.48, 9, 13, 15, 3.22, 0, -1, 1])
In [4]: indices = a.argsort()[::-1]
Out[4]: array([ 2, 6, 5, 4, 7, 1, 10, 8, 9, 0, 3])
In [5]: a[indices]
Out[5]:
array([ 15.827, 15. , 13. , 9. , 3.22 , 2.73 , 1. ,
0. , -1. , -1. , -8.48 ])
另外,让我使用您原始问题中提供的信息添加以下片段作为答案 我想对索引进行排序,以便 在索引中a在b之前,然后是mylist[a]>mylist[b] …以及您在评论中提供的其他信息
另外,如果mylist是一个 numpy浮动 然后你可以简单地做:
In [2]: import numpy
In [3]: a = numpy.asarray([-1, 2.73, 15.827, -8.48, 9, 13, 15, 3.22, 0, -1, 1])
In [4]: indices = a.argsort()[::-1]
Out[4]: array([ 2, 6, 5, 4, 7, 1, 10, 8, 9, 0, 3])
In [5]: a[indices]
Out[5]:
array([ 15.827, 15. , 13. , 9. , 3.22 , 2.73 , 1. ,
0. , -1. , -1. , -8.48 ])
Python 2.6+,最优雅的答案:
indices.sort(key = lambda x:mylist[x], reverse = True)
Python 2.6+,最优雅的答案:
indices.sort(key = lambda x:mylist[x], reverse = True)
另外,如果mylist是一个numpy浮动列表呢?然后,我需要一个整数返回符号函数。
int(numpy.sign(…)
是我能做的最好的了吗?请记住P3K有点不同,如果您感兴趣,它会提供更多信息这是有效的:indexs.sort(key=lambda x:mylist[x],reverse=True)
另外,如果mylist是一个numpy float列表呢?然后,我需要一个整数返回符号函数。int(numpy.sign(…)
是我能做的最好的了吗?请记住P3K有点不同,如果您感兴趣,它有更多信息这是有效的:index.sort(key=lambda x:mylist[x],reverse=True)