0

我想生成一个向量来告诉我应该如何对列表进行排序,因此我可以将其应用于与对象列表关联的标签列表。我知道在matlab中是可能的,在python中是否存在这样的东西?

一个例子:

假设您有一个二进制数字列表list1 = ['10100', '10101', '01010', '00010', '00100'] 和一系列二进制数字标签,colnames = ["A","B","C","D","E"]使用list.sort()I can sort list1 in place,但我想知道该排序是如何完成的,然后将该排列也应用于标签列表。

4

1 回答 1

1

像这样的东西?

>>> listOne = ['10100', '10101', '01010', '00010', '00100']
>>> listTwo = ["A","B","C","D","E"]
>>> listThree = zip(listOne, listTwo)
>>> listThree.sort(key = lambda x : x[0])
>>> listThree
[('00010', 'D'), ('00100', 'E'), ('01010', 'C'), ('10100', 'A'), ('10101', 'B')]
>>> listOneSorted, listTwoSorted = zip(*listThree)
>>> listOneSorted
('00010', '00100', '01010', '10100', '10101')
>>> listTwoSorted
('D', 'E', 'C', 'A', 'B')

或者总结以上所有内容的一个班轮。

>>> listOneSorted, listTwoSorted = zip(*sorted(zip(listOne, listTwo), key = lambda x : x[0]))
于 2013-07-12T17:02:08.697 回答