並べ替えは常に、多くのアプリケーションで実行される重要な操作であり、多くの問題のサブ問題としても実行されます。多くのバリエーションとテクニックが議論されており、それらの知識はプログラミング中に役立つ可能性があります。この記事では、リストを含むリストの並べ替えについて説明します。これを実行する特定の方法について説明しましょう。
方法 #1 :sorted() + リスト内包表記を使用する
この方法では、適用できる長いプロセスの省略表現を使用するだけです。リストは反復され、内部リストもソートされる関数sortedを使用して後続のサブリストがソートされます。
C の階乗
ステップ バイ ステップ アプローチ:
- まず、2 つのサブリストを含むネストされたリスト test_list を初期化します。各サブリストには異なる数のリストが含まれ、各サブリストには 2 つの整数が含まれます。
- 次に、print() 関数と str() 関数を使用して元のリストを印刷し、リストを印刷用の文字列に変換します。
- 各サブリスト内の要素に基づいて test_list 内の各サブリストを並べ替えるには、sorted() 関数でリスト内包表記を使用します。
- 結果として得られるソートされたリストのリストを変数 res に代入します。
- 最後に、print() 関数と str() 関数を使用して、結果のソートされたリスト res を出力し、リストを印刷用の文字列に変換します。
Python3
# Python3 code to demonstrate> # Sorting list of lists with similar list elements> # using list comprehension + sorted()> # initializing list> test_list>=> [[[>4>,>4>], [>1>,>1>]], [[>3>,>3>], [>2>,>2>], [>5>,>5>]]]> # printing original list> print>(>'The original list : '> +> str>(test_list))> # using list comprehension + sorted()> # Sorting list of lists with similar list elements> res>=> [>sorted>(idx)>for> idx>in> test_list]> # print result> print>(>'The list after performing sort operation : '> +> str>(res))> |
>
>出力:
元のリスト:[[[4, 4], [1, 1]], [[3, 3], [2, 2], [5, 5]]] ソート後のリスト:[[[1 、1]、[4、4]]、[[2、2]、[3、3]、[5、5]]]
時間計算量: O(nlogn)
補助スペース: O(1)
方法 #2: map() +sorted() を使用する
上記の関数を組み合わせた場合も、上記の方法と同様のタスクを実行しますが、唯一の違いは、並べ替えロジックをサブリスト全体に拡張するためにマップ関数が使用されることです。
Python3
# Python3 code to demonstrate> # Sorting list of lists with similar list elements> # using map() + sorted()> # initializing list> test_list>=> [[[>4>,>4>], [>1>,>1>]], [[>3>,>3>], [>2>,>2>], [>5>,>5>]]]> # printing original list> print>(>'The original list : '> +> str>(test_list))> # using map() + sorted()> # Sorting list of lists with similar list elements> res>=> list>(>map>(>sorted>, test_list))> # print result> print>(>'The list after performing sort operation : '> +> str>(res))> |
コンピューターが発明されたのは何年ですか
>
>出力:
元のリスト:[[[4, 4], [1, 1]], [[3, 3], [2, 2], [5, 5]]] ソート後のリスト:[[[1 、1]、[4、4]]、[[2、2]、[3、3]、[5、5]]]
時間計算量: O(n*nlogn)、n はリスト test_list 内の要素の数です。
補助スペース: O(n)、n はリスト test_list 内の要素の数です。
方法 3: sort() メソッドでラムダ関数を使用する。
アプローチ:
- リスト test_list をサンプル データで初期化します。
- print() 関数を使用して元のリストを出力します。
- リストのリストを並べ替えるには、sort() メソッドを使用します。 key パラメーターは、各リスト要素を並べ替えるラムダ関数に設定されます。
- print() 関数を使用して、ソートされたリストを出力します。
上記のアプローチの実装を以下に示します。
Python3
# Python3 code to demonstrate> # Sorting list of lists with similar list elements> # using lambda function and sort()> # initializing list> test_list>=> [[[>4>,>4>], [>1>,>1>]], [[>3>,>3>], [>2>,>2>], [>5>,>5>]]]> # printing original list> print>(>'The original list : '> +> str>(test_list))> # using lambda function and sort()> # Sorting list of lists with similar list elements> test_list.sort(key>=>lambda> x:>sorted>(x))> # print result> print>(>'The list after performing sort operation : '> +> str>(test_list))> |
>
>出力
The original list : [[[4, 4], [1, 1]], [[3, 3], [2, 2], [5, 5]]] The list after performing sort operation : [[[4, 4], [1, 1]], [[3, 3], [2, 2], [5, 5]]]>
時間計算量: O(n log n)。n はリスト内の要素の数です。
補助スペース: O(1)。入力リスト以外に余分なスペースを使用しないためです。
方法 #4: functools.cmp_to_key() を使用する
- functools モジュールをインポートします。
- 2 つのサブリストを引数として受け取り、それらの相対順序に応じて -1、0、または 1 を返す比較関数を定義します。
- functools のsorted() 関数と cmp_to_key() 関数を使用して、比較関数を使用して元のリストを並べ替えます。
Python3
コンピューターネットワーク
# Python3 code to demonstrate> # Sorting list of lists with similar list elements> # using functools.cmp_to_key()> # import functools module> import> functools> # initializing list> test_list>=> [[[>4>,>4>], [>1>,>1>]], [[>3>,>3>], [>2>,>2>], [>5>,>5>]]]> # printing original list> print>(>'The original list : '> +> str>(test_list))> # define comparison function> def> compare_lists(list1, list2):> >if> sorted>(list1) <>sorted>(list2):> >return> ->1> >elif> sorted>(list1)>>>sorted>(list2):> >return> 1> >else>:> >return> 0> # using functools.cmp_to_key() and sorted()> # Sorting list of lists with similar list elements> test_list.sort(key>=>functools.cmp_to_key(compare_lists))> # print result> print>(>'The list after performing sort operation : '> +> str>(test_list))> |
>
>出力
The original list : [[[4, 4], [1, 1]], [[3, 3], [2, 2], [5, 5]]] The list after performing sort operation : [[[4, 4], [1, 1]], [[3, 3], [2, 2], [5, 5]]]>
時間計算量: O(N * M * log(M))、 ここで、N はサブリストの数、M は最長のサブリストの長さです。
補助スペース:O(M) ここで、M は並べ替え操作の最長のサブリストの長さです。