0

事前に定義された特定の順序で並べ替えたいタプルのリストがあります。

Tuple_list=[(0, 1), (0, 3), (1, 3), (2, 0), (2, 3), (3, 1), (3, 2)]

order=[5,7,4,1,6,2,3]

sorted_tuples=[(2,3), (3,2), (2,0), (0,1), (3,1), (0,3), (1,3)]

順序は、リスト内のタプルの位置を参照します。Python の sorted 関数を使用しようとしましたが、目的の出力を生成することができず、この特定のタイプのタプルの並べ替えの問題を解決するスレッドが見つかりません。

4

3 に答える 3

4

リスト内包表記を使用します。

>>> [Tuple_list[i-1] for i in order]
[(2, 3), (3, 2), (2, 0), (0, 1), (3, 1), (0, 3), (1, 3)]
于 2013-10-14T17:02:34.490 に答える
0

この「並べ替え」を順列と呼びます

sorted_tuples = map(lambda o: Tuple_list[o-1], order)
于 2013-10-14T17:04:27.773 に答える
0

You don't need to use a sorting algorithm if you already know what elements to take in what order -- just iterate over the indices instead and get the corresponding list items:

sorted_tuples = [tuple_list[i - 1] for i in order]

or, if you use real zero-based Python indices instead

sorted_tuples = operator.itemgetter(*order)(tuple_list)
于 2013-10-14T17:04:48.613 に答える