Python set arithmetic

Organize the set operation method in Python set.

set

X = set([1,2,3,4,5,6,1,1,1])
print(X)
# {1, 2, 3, 4, 5, 6}

Y = {9,9,9,4,5,6,7,8,9}
print(Y)
# {4, 5, 6, 7, 8, 9}

<set>.union(set)

Z = X.union(Y)
print(Z)
# {1, 2, 3, 4, 5, 6, 7, 8, 9}

Z = X | Y
print(Z)
# {1, 2, 3, 4, 5, 6, 7, 8, 9}

<set>.intersection(set)

Z = X.intersection(Y)
print(Z)
# {4, 5, 6}

Z = X & Y
print(Z)
# {4, 5, 6}

<set>.symmetric_difference(set)

Z = X.symmetric_difference(Y)
print(Z)
# {1, 2, 3, 7, 8, 9}

Z = X ^ Y
print(Z)
# {1, 2, 3, 7, 8, 9}

<set>.difference(set)

Z = X.difference(Y)
print(Z)
# {1, 2, 3}

Z = X - Y
print(Z)
# {1, 2, 3}

<set>.issubset(set)

X = {1,2,3}
Y = {1,2,3,4,5}

Z = X.issubset(Y)
print(Z)
# True

Z = X <= Y
print(Z)
# True

<set>.issuperset(set)

X = {1,2,3,4,5}
Y = {1,2,3}

Z = X.issuperset(Y)
print(Z)
# True

Z = X >= Y
print(Z)
# True

<set>.isdisjoint(set)

X = {1,2,3}
Y = {4,5,6}

Z = X.isdisjoint(Y)
print(Z)
# True

Remarks

This article is a migration article from the blog "Technical notes for chores engineers". The previous blog will be deleted.

Recommended Posts

Python set arithmetic
Python set arithmetic
Python date arithmetic
Set up Python 3.4 on Ubuntu
gnubg python scripting: set threads
Four arithmetic operations in python
Python
Set Up for Mac (Python)
Set python test in jenkins
Set python compile options with pyenv
[Python] ARC006A (set strongest theory) [AtCoder]
Set up Python environment on CentOS
Solving Verbal Arithmetic (Ruby / Python Recreation)
kafka python
Python basics ⑤
python + lottery 6
Python Summary
Built-in python
Python technique
Studying python
[Python] Set the graph range with matplotlib
Python 2.7 Countdown
Python memorandum
python tips
python function ①
Python basics
Python memo
ufo-> python (3)
install python
Python Singleton
Python> empty XXX (XXX: dict, list, tuple, set)> {} / [] / () / set ()
Python basics ④
Python Memorandum 2
python memo
Set comprehension
Python Jinja2
Python increment
atCoder 173 Python
[Python] function
Python installation
python tips
Installing Python 3.4.3.
Try python
Python memo
To set default encoding to utf-8 in python
Python iterative
Python algorithm
Python2 + word2vec
[Python] Variables
Python functions
Python sys.intern ()
Python tutorial
Python decimals
python underscore
Python summary
Start python
[Python] Sort
Associate the table set in python models.py
Note: Python
Python basics ③
python log