さまざまなセット操作を説明するPythonプログラム

Python

Pythonは、要素が一意である必要があるsetと呼ばれるデータ型を提供します。 和集合、共通部分、差、対称差などのさまざまなセット操作を実行するために使用できます。

ソースコード

# Program to perform different set operations like in mathematics

# define three sets
E = {0, 2, 4, 6, 8};
N = {1, 2, 3, 4, 5};

# set union
print("Union of E and N is",E | N)

# set intersection
print("Intersection of E and N is",E & N)

# set difference
print("Difference of E and N is",E - N)

# set symmetric difference
print("Symmetric difference of E and N is",E ^ N)

出力

Union of E and N is {0, 1, 2, 3, 4, 5, 6, 8}
Intersection of E and N is {2, 4}
Difference of E and N is {8, 0, 6}
Symmetric difference of E and N is {0, 1, 3, 5, 6, 8}

このプログラムでは、2つの異なるセットを取得し、それらに対して異なるセット操作を実行します。 これは、setメソッドを使用して同等に実行できます。



Hope this helps!

Source link

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です