Pythonの集合の差分をdifferenceメソッドやマイナス演算子を使って求める方法

スポンサーリンク

Python Set Difference は基本的に反復可能なオブジェクトに対して差分操作を行い、その結果にはそれらの間の差分が含まれます。

これは、(操作が呼び出された)セットに存在し、他のセットには存在しない要素を含んでいます。

スポンサーリンク

Python集合差分ベン図

以下のベン図は、どんなテキストよりも集合の差分をよりよく理解することができます。

Set_A = {1, 3, 5, 7, 9}
 
Set_B = {2, 4, 6, 7, 9}
 
Result = Set_A.difference(Set_B)
print(Result);
 
input_list = [1, 2, 3, 4, 5, 6, 7] #list
Display = Set_A.difference(input_list)
print(Display)

セット差分を求めるテクニック

Pythonで複数の集合の集合差を求めるテクニックを以下に示します。

  • set difference() メソッドを使用します。
  • 演算子”-“を使用する
  • difference_update メソッドによる方法
    -=” 演算子

1. Python 差分()メソッドによる集合差分

difference() メソッドは通常、String, List, Tuples などの反復記号に対して操作します。

difference() メソッドは、メソッドが呼び出された特定のセットから、他のセットには存在しないすべての項目を含む新しいセットを出力として生成されます。

たとえば

セット A = {1, 9, 5, 7} とセット B = {2, 8, 9, 7} の場合。

このとき、集合Aの差集合Bは、集合Aに存在し集合Bに存在しないすべての要素を含むことになる。

注意: set 以外の反復記号が引数として difference() メソッドに渡された場合、このメソッドはまず反復記号オブジェクトを sets に変換してから処理を実行します。

構文は以下の様な感じです。

Set.difference(iterable_object) = Set Object

例えば、以下の様になります。

Set_A = {1, 3, 5, 7, 9}
 
Set_B = {2, 4, 6, 7, 9}
 
Result = Set_A - Set_B
print(Result)

結果は以下の通りです。

{1, 3, 5}
{9}

2. Pythonで”-“演算子を使った集合差分演算

演算子「-」は、要素の集合差分演算を行う際にも使用できます。

これは、difference()メソッドと同じ目的です。

difference()メソッドとの唯一の違いは、”-“演算子は集合の要素に対してのみ動作し、後者は任意の反復可能なオブジェクトに対して動作することです。

構文は以下の様な感じです。

SetA - SetB = Resultant_Set

例えば、以下の様になります。

Set_A = {1, 3, 5, 7, 9}
 
Set_B = {2, 4, 6, 7, 9}
 
print("Set_A before the difference operation: ", Set_A)
 
Set_A.difference_update(Set_B)
 
print("Set_A difference Set_B: ", Set_A);

結果は以下の通りです。

{1, 3, 5}

3. difference_update()メソッドの使用法

difference_update()メソッドは、String、Lists、Tuples などの反復可能なオブジェクトからセット要素の差分を返すこともできます。

difference_update() メソッドでは、差分演算の結果を新たに作成したセットに格納するのではなく、演算を行ったセットと同じセットを更新します。

構文は以下の様な感じです。

構文:“`
Set.difference_update(iterable_object)

例

<div class="wp-block-syntaxhighlighter-code"><div><div class="syntaxhighlighter nogutter python" id="highlighter_98153"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="python plain">Set_A </code><code class="python keyword">=</code> <code class="python plain">{</code><code class="python value">1</code><code class="python plain">, </code><code class="python value">3</code><code class="python plain">, </code><code class="python value">5</code><code class="python plain">, </code><code class="python value">7</code><code class="python plain">, </code><code class="python value">9</code><code class="python plain">}</code></div><div class="line number2 index1 alt1"> </div><div class="line number3 index2 alt2"><code class="python plain">Set_B </code><code class="python keyword">=</code> <code class="python plain">{</code><code class="python value">2</code><code class="python plain">, </code><code class="python value">4</code><code class="python plain">, </code><code class="python value">6</code><code class="python plain">, </code><code class="python value">7</code><code class="python plain">, </code><code class="python value">9</code><code class="python plain">}</code></div><div class="line number4 index3 alt1"> </div><div class="line number5 index4 alt2"><code class="python functions">print</code><code class="python plain">(</code><code class="python string">"Set_A before the difference operation: "</code><code class="python plain">, Set_A)</code></div><div class="line number6 index5 alt1"> </div><div class="line number7 index6 alt2"><code class="python plain">Set_A </code><code class="python keyword">-</code><code class="python keyword">=</code> <code class="python plain">Set_B</code></div><div class="line number8 index7 alt1"> </div><div class="line number9 index8 alt2"><code class="python functions">print</code><code class="python plain">(</code><code class="python string">"Set_A difference Set_B: "</code><code class="python plain">, Set_A);</code></div></div></td></tr></tbody></table></div></div></div>

出力

Set_A before the difference operation: {9, 1, 3, 5, 7}
Set_A difference Set_B: {1, 3, 5}

### 4. 4. "-=" 演算子の使用

演算子は、difference_updateメソッドと同様に、集合の要素に対して差分演算を行い、差分演算が行われた集合内の結果を更新します。

注意: "-=" 演算子は、セットオブジェクトに対してのみ動作します。

構文

Set_A -= Set_B

例

<div class="wp-block-image"><figure class="aligncenter size-full"><img alt="Set Difference" class="wp-image-2907" data-lazyloaded="1" data-sizes="(max-width: 493px) 100vw, 493px" data-src="https://www.askpython.com/wp-content/uploads/2020/01/Set-Difference.png" data-srcset="https://www.askpython.com/wp-content/uploads/2020/01/Set-Difference.png 493w, https://www.askpython.com/wp-content/uploads/2020/01/Set-Difference-300x209.png.webp 300w" height="344" src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI0OTMiIGhlaWdodD0iMzQ0IiB2aWV3Qm94PSIwIDAgNDkzIDM0NCI+PHJlY3Qgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgZmlsbD0iI2NmZDRkYiIvPjwvc3ZnPg==" width="493"/><figcaption>Set Difference</figcaption></figure></div>

出力

Set_A before the difference operation: {9, 1, 3, 5, 7}
Set_A difference Set_B: {1, 3, 5}
“`


まとめ

以上、今回はPythonで集合の要素の差を求めるさまざまな方法について理解しました。


タイトルとURLをコピーしました