logo

パイソン | 2 つのリストの交差

2 つのリストの共通部分は、最初のリストの両方に共通する要素をすべて取得し、それらを別のリストに格納する必要があることを意味します。 Python には、リストの交差を実行できるさまざまな方法があります。
例:

Input : lst1 = [15, 9, 10, 56, 23, 78, 5, 4, 9] lst2 = [9, 4, 5, 36, 47, 26, 10, 45, 87] Output : [9, 10, 4, 5] Input : lst1 = [4, 9, 1, 17, 11, 26, 28, 54, 69] lst2 = [9, 9, 74, 21, 45, 11, 63, 28, 26] Output : [9, 11, 26, 28]>

方法 1:
これは、組み込み関数を使用しない最も単純な方法です。



Python3




JSで寝る



# Python program to illustrate the intersection> # of two lists in most simple way> def> intersection(lst1, lst2):> >lst3>=> [value>for> value>in> lst1>if> value>in> lst2]> >return> lst3> # Driver Code> lst1>=> [>4>,>9>,>1>,>17>,>11>,>26>,>28>,>54>,>69>]> lst2>=> [>9>,>9>,>74>,>21>,>45>,>11>,>63>,>28>,>26>]> print>(intersection(lst1, lst2))>



>

>

出力:

[9, 11, 26, 28]>

方法 2:
この方法には、次の使用が含まれます。 set() メソッド

Python3




# Python program to illustrate the intersection> # of two lists using set() method> def> intersection(lst1, lst2):> >return> list>(>set>(lst1) &>set>(lst2))> # Driver Code> lst1>=> [>15>,>9>,>10>,>56>,>23>,>78>,>5>,>4>,>9>]> lst2>=> [>9>,>4>,>5>,>36>,>47>,>26>,>10>,>45>,>87>]> print>(intersection(lst1, lst2))>

>

>

出力:

[9, 10, 4, 5]>

上記のプログラムの時間計算量は O(n) です。ここで、n は lst1 と lst2 の間の長いリストの長さです。

プログラムの空間計算量は O(n) です。ここで、n は lst1 と lst2 の間の小さいリストの長さです。

方法 3:
この方法では、 set() より大きなリスト 次に、という組み込み関数を使用します。 交差点() 交差リストを計算します。 交差点() セットのファーストクラスの部分です。

Python3




# Python program to illustrate the intersection> # of two lists using set() and intersection()> def> Intersection(lst1, lst2):> >return> set>(lst1).intersection(lst2)> > # Driver Code> lst1>=> [>4>,>9>,>1>,>17>,>11>,>26>,>28>,>28>,>26>,>66>,>91>]> lst2>=> [>9>,>9>,>74>,>21>,>45>,>11>,>63>]> print>(Intersection(lst1, lst2))>

>

>

出力:

{9, 11}>

方法 4:
これを利用することで ハイブリッド方式 プログラムの複雑さは O(n) に下がります。これは、次のプログラムを実行する効率的な方法です。

Python3




# Python program to illustrate the intersection> # of two lists> def> intersection(lst1, lst2):> ># Use of hybrid method> >temp>=> set>(lst2)> >lst3>=> [value>for> value>in> lst1>if> value>in> temp]> >return> lst3> # Driver Code> lst1>=> [>9>,>9>,>74>,>21>,>45>,>11>,>63>]> lst2>=> [>4>,>9>,>1>,>17>,>11>,>26>,>28>,>28>,>26>,>66>,>91>]> print>(intersection(lst1, lst2))>

>

>

出力:

[9, 9, 11]>

方法 5:
これは、他のリスト内のサブリストに対して交差が実行される場所です。ここでは、次の概念を使用しました。 フィルター()。

Python3




# Python program to illustrate the intersection> # of two lists, sublists and use of filter()> def> intersection(lst1, lst2):> >lst3>=> [>list>(>filter>(>lambda> x: x>in> lst1, sublist))>for> sublist>in> lst2]> >return> lst3> # Driver Code> lst1>=> [>1>,>6>,>7>,>10>,>13>,>28>,>32>,>41>,>58>,>63>]> lst2>=> [[>13>,>17>,>18>,>21>,>32>], [>7>,>11>,>13>,>14>,>28>], [>1>,>5>,>6>,>8>,>15>,>16>]]> print>(intersection(lst1, lst2))>

>

>

働く: フィルター部分は各サブリストの項目を取得し、それがソース リストにあるかどうかを確認します。リスト内包表記は、list2 のサブリストごとに実行されます。
出力:

[[13, 32], [7, 13, 28], [1, 6]]>

方法 6:reduce() を使用する:
アルゴリズム:

  1. functools モジュールからreduce関数をインポートします。
  2. 2 つのリストを定義します。
  3. 変数交差を空のリストで初期化します。
  4. reduce 関数を使用して、lst1 の要素を反復処理します。
  5. ラムダ関数内で、現在の要素が lst2 に存在し、交差リストにまだ存在していないかどうかを確認します。
  6. 存在する場合は、現在の要素を交差リストに追加します。
  7. 交差リストを返します。
  8. 交差点リストを印刷します。

Python3




from> functools>import> reduce> lst1>=> [>15>,>9>,>10>,>56>,>23>,>78>,>5>,>4>,>9>]> lst2>=> [>9>,>4>,>5>,>36>,>47>,>26>,>10>,>45>,>87>]> intersection>=> reduce>(>lambda> acc, x: acc>+> [x]>if> x>in> lst2>and> x>not> in> acc>else> acc, lst1, [])> print>(intersection)> #This code is contributed by Rayudu.>

>

>

出力

[9, 10, 5, 4]>

時間計算量: O(n^2)、 ここで、n は lst1 の長さです。
空間の複雑さ: O(n)、 ここで、n は lst1 の長さです。