Python set arithmetic

Introduction

I'll soon forget about the set

What is a set?

--A set is a collection of unordered elements that have no overlapping elements. -Generated by placing elements inside {} separated by commas or by passing a sequence type to the set function

Operation type Description
Sum (union) Included in A or B
Product (intersection) Common to A and B
Difference Included only in A
Symmetric difference Included only in A or B

The following mathematical operations can be performed using the Set object.

Sum (union)

Included in A or B

>>> {1, 2, 3} | {2, 3, 4}  #sum
{1, 2, 3, 4}

Or

>>> a = {1, 2, 3}
>>> b = {2, 3, 4}
>>> a.union(b)
{1, 2, 3, 4}

When changing the original set

>>> a = {1, 2, 3}
>>> b = {2, 3, 4}
>>> a.update(b)
{1, 2, 3, 4}
>>> a
{1, 2, 3, 4}

Product (intersection)

Common to A and B

>>> {1, 2, 3} & {2, 3, 4}  #product
{2, 3}

Or

>>> a = {1, 2, 3}
>>> b = {2, 3, 4}
>>> a.intersection(b)
{2, 3}

When changing the original set

>>> a = {1, 2, 3}
>>> b = {2, 3, 4}
>>> a.intersection_update(b)
>>> a
{2, 3}

Difference

Included only in A

>>> {1, 2, 3} - {2, 3, 4}  #difference
{1}

Or

>>> a = {1, 2, 3}
>>> b = {2, 3, 4}   
>>> a.difference(b)
{1}

When changing the original set

>>> a = {1, 2, 3}
>>> b = {2, 3, 4}
>>> a.difference_update(b)
>>> a
{1}

Symmetric difference

Included only in A or B

>>> {1, 2, 3} ^ {2, 3, 4}  #Target difference
{1, 4}

Or

>>> a = {1, 2, 3}
>>> b = {2, 3, 4}
>>> a.symmetric_difference(b)
{1, 4}

When changing the original set

>>> a = {1, 2, 3}
>>> b = {2, 3, 4}
>>> a.symmetric_difference_update(b)
>>> a
{1, 4}

Determining if it is a subset

When all the elements on the left side are included in the set on the right side

>>> {1, 3} <= {1, 2, 3}
True

When not all the elements on the left side are included in the set on the right side

>>> {1, 4} <= {1, 2, 3}
False

in conclusion

Enjoy Python ~

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 comprehension
Python technique
Studying python
[Python] Set the graph range with matplotlib
Python 2.7 Countdown
Python memorandum
Python FlowFishMaster
Python service
python tips
python function ①
Python basics
Python memo
ufo-> python (3)
Python comprehension
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