在python中,如何根据多维列表的第一行字母按字母顺序对其排序
如何在不使用numpy或其他模块的情况下,根据多维列表的第一行数据按字母顺序对其排序 例如,我有一个列表:在python中,如何根据多维列表的第一行字母按字母顺序对其排序,python,arrays,algorithm,sorting,multidimensional-array,Python,Arrays,Algorithm,Sorting,Multidimensional Array,如何在不使用numpy或其他模块的情况下,根据多维列表的第一行数据按字母顺序对其排序 例如,我有一个列表: [['M', 'A', 'R', 'K'] [1, 3, 5, 4] [2, 6, 7, 8]] 我希望它像: [['A', 'K', 'M', 'R'] [3, 4, 1, 5] [6, 8, 2, 7]] 谢谢 试试numpy >>> import numpy as np >>> a
[['M', 'A', 'R', 'K']
[1, 3, 5, 4]
[2, 6, 7, 8]]
我希望它像:
[['A', 'K', 'M', 'R']
[3, 4, 1, 5]
[6, 8, 2, 7]]
谢谢 试试numpy
>>> import numpy as np
>>> a = np.array([['M', 'A', 'R', 'K'],
... [1, 3, 5, 4],
... [2, 6, 7, 8]], dtype=object)
>>> a[:,np.argsort(a[0])]
array([['A', 'K', 'M', 'R'],
[3, 4, 1, 5],
[6, 8, 2, 7]], dtype=object)
没有numpy:
>>> map(list,zip(*sorted(zip(*a))))
[['A', 'K', 'M', 'R'], [3, 4, 1, 5], [6, 8, 2, 7]]
用numpy试试
>>> import numpy as np
>>> a = np.array([['M', 'A', 'R', 'K'],
... [1, 3, 5, 4],
... [2, 6, 7, 8]], dtype=object)
>>> a[:,np.argsort(a[0])]
array([['A', 'K', 'M', 'R'],
[3, 4, 1, 5],
[6, 8, 2, 7]], dtype=object)
没有numpy:
>>> map(list,zip(*sorted(zip(*a))))
[['A', 'K', 'M', 'R'], [3, 4, 1, 5], [6, 8, 2, 7]]
我认为应该有更好的办法;不管怎么说,这会起作用:
>>> l = [['M', 'A', 'R', 'K'], [1, 3, 5, 4], [2, 6, 7, 8]]
>>> sorted_l = map(list, zip(*sorted(zip(*l))))
>>> print(list(sorted_l))
[['A', 'K', 'M', 'R'], [3, 4, 1, 5], [6, 8, 2, 7]]
编辑:
zip(*l)
提供了一个迭代器(在Python 3中),如果您使用[i for i in zip(*l)]
,您会发现它看起来像[('M',1,2),('A',3,6),('R',5,7),('K',4,8)]
sorted(zip(*l))
对上面的列表进行排序(变成[('A',3,6),('K',4,8),('M',1,2),('R',5,7)]
在引擎盖下),我没有指定键,所以它直接比较元素l
中的元素是('A','K','M','R'),(3,4,1,5),(6,8,2,7)
;但是,您必须执行map()
,这样才能获得
而不是
map()
文献:,和我认为应该有更好的方法;不管怎么说,这会起作用:
>>> l = [['M', 'A', 'R', 'K'], [1, 3, 5, 4], [2, 6, 7, 8]]
>>> sorted_l = map(list, zip(*sorted(zip(*l))))
>>> print(list(sorted_l))
[['A', 'K', 'M', 'R'], [3, 4, 1, 5], [6, 8, 2, 7]]
编辑:
zip(*l)
提供了一个迭代器(在Python 3中),如果您使用[i for i in zip(*l)]
,您会发现它看起来像[('M',1,2),('A',3,6),('R',5,7),('K',4,8)]
sorted(zip(*l))
对上面的列表进行排序(变成[('A',3,6),('K',4,8),('M',1,2),('R',5,7)]
在引擎盖下),我没有指定键,所以它直接比较元素l
中的元素是('A','K','M','R'),(3,4,1,5),(6,8,2,7)
;但是,您必须执行map()
,这样才能获得
而不是
map()
['M','A','R','K']
。为了对这些字母进行排序,您应该首先获取索引1
,然后获取索引3
,然后获取索引0
,最后获取索引2
:(akmr)
。找到映射后,只需调用itemgetter
即可按该顺序获取每行上的项目
import operator
a=[
['M', 'A', 'R', 'K'],
[1, 3, 5, 4],
[2, 6, 7, 8],
]
sorted_first = sorted(a[0])
sorted_indexes = [a[0].index(letter) for letter in sorted_first]
print "Mapping: %s" % sorted_indexes
new_sorted = [operator.itemgetter(* sorted_indexes)(line) for line in a]
print new_sorted
哪些输出:
Mapping: [1, 3, 0, 2]
[('A', 'K', 'M', 'R'),
(3, 4, 1, 5),
(6, 8, 2, 7)]
编辑:
让我看到(在对这个答案的评论中)有比使用O(n2)更有效的方法来计算sorted_索引
变量:
一旦您知道排序索引的新“顺序”(或映射),您应该能够基本上遍历所有行,并按照刚刚找到的顺序获取项目
在您的例子中,您有['M','A','R','K']
。为了对这些字母进行排序,您应该首先获取索引1
,然后获取索引3
,然后获取索引0
,最后获取索引2
:(akmr)
。找到映射后,只需调用itemgetter
即可按该顺序获取每行上的项目
import operator
a=[
['M', 'A', 'R', 'K'],
[1, 3, 5, 4],
[2, 6, 7, 8],
]
sorted_first = sorted(a[0])
sorted_indexes = [a[0].index(letter) for letter in sorted_first]
print "Mapping: %s" % sorted_indexes
new_sorted = [operator.itemgetter(* sorted_indexes)(line) for line in a]
print new_sorted
哪些输出:
Mapping: [1, 3, 0, 2]
[('A', 'K', 'M', 'R'),
(3, 4, 1, 5),
(6, 8, 2, 7)]
编辑:
让我看到(在对这个答案的评论中)有比使用O(n2)更有效的方法来计算sorted_索引
变量:
你试过什么吗?包括您尝试过使用nummy的内容,如下面的示例,但如果不使用nummy,我还没有找到解决方案。这应该是您问题的一部分,就像您不使用“nummy”所希望的那样?那会让你明白你想要什么?谢谢你试过什么吗?包括您尝试过使用nummy的内容,如下面的示例,但如果不使用nummy,我还没有找到解决方案。这应该是您问题的一部分,就像您不使用“nummy”所希望的那样?这会让你清楚地知道你想要什么吗?谢谢,有没有不使用numpy的方法呢?有没有不使用numpy的方法呢?在每个元素的列表中搜索排序后的索引的位置不是很有效的O(n^2)。4项列表只是一个例子。你怎么知道?根据你的说法,还有什么更有效的方法呢?OP说“例如”和
sorted\u index=sorted(range(len(a[0]),key=lambda i:a[0][i])
将是O(nlog n),就像公认的答案一样。该死!你说得对。我试着这样做,给了我一些错误。。。使用索引
。在每个元素的列表中搜索排序的\u索引的位置
不是很有效O(n^2)。4项列表只是一个示例。您如何知道?根据你的说法,还有什么更有效的方法呢?OP说“例如”和sorted\u index=sorted(range(len(a[0]),key=lambda i:a[0][i])
将是O(nlog n),就像公认的答案一样。该死!你说得对。我试着这样做,给了我一些错误。。。使用索引
。