[Python] I tried to summarize the set type (set) in an easy-to-understand manner.

Why I wrote this article

I am studying Python with the aim of becoming an AI human resource, but since the set type content was the content of a set of numbers A, I thought that I would use it in the future, so I will summarize it.

I am using this to study Python. Learning roadmap for Python beginners [You can also study by yourself on your blog]

Introduction to Python 3 taught by active Silicon Valley engineers + Applications + American Silicon Valley style code style

Set operation with Python, set type (union, intersection and subset judgment, etc.)

What is a set type?

--A mass of elements that are not duplicated, have no order, and have no number defined. ――It feels like a relative of a sequence type (string, tuple, list) -Define by separating with commas in {} --Define by changing the data type to a set type (set type) with the set () function

I tried to illustrate

Suppose there are two aggregate types a and b.

--Sets common to a and b (intersection): ʻa & b --A set (difference set) excluding "elements common to a and b (product set)" from a: ʻa --b --A set (difference set) excluding "elements common to a and b (product set)" from b: b --a

Is the image of the figure below. IMG_1621.jpg

--A set (union) that combines all the elements of a and b: ʻa | b`

Is the image of the figure below IMG_1622.jpg

-A set that combines "things that belong to a and not belong to b" and "things that belong to b and do not belong to a" (symmetric difference set): ʻa ^ b`

Is the image of the figure below IMG_1623.jpg

Aggregate calculation method

Let's actually express what was illustrated earlier in code.

set.py


#Define aggregate types a and b
a = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
b = {2, 4, 6, 8, 10, 12} 

#Intersection
print(a & b)  #Execution result:{2, 4, 6, 8, 10}

#Difference set
print(a - b)  #Execution result:{1, 3, 5, 7, 9}
print(b - a)  #Execution result:{12}

#Union
print(a | b)  #Execution result:{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12}

#Symmetric set
print(a ^ b) #Execution result:{1, 3, 5, 7, 9, 12}

By the way, methods can also be used for collective operations.

set.py


#Define aggregate types a and b
a = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
b = {2, 4, 6, 8, 10, 12} 

#Intersection
print(a & b)  #Execution result:{2, 4, 6, 8, 10}
print(a.intersection(b))  #Execution result:{2, 4, 6, 8, 10}

#Difference set
print(a - b)  #Execution result:{1, 3, 5, 7, 9}
print(a.difference(b))  #Execution result:{1, 3, 5, 7, 9}

print(b - a)  #Execution result:{12}
print(b.difference(a))  #Execution result:{12}

#Union
print(a | b)  #Execution result:{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12}
print(a.union(b)) #Execution result:{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12}

#Symmetric set
print(a ^ b) #Execution result:{1, 3, 5, 7, 9, 12}
print(a.symmetric_difference(b)) #Execution result:{1, 3, 5, 7, 9, 12} 

in conclusion

I think I'll summarize the subsets in another article. After all, it is recommended to output something because you can organize your mind.

Python is fun!

Recommended Posts

[Python] I tried to summarize the set type (set) in an easy-to-understand manner.
I tried to summarize Cpaw Level1 & Level2 Write Up in an easy-to-understand manner
I tried to summarize Cpaw Level 3 Write Up in an easy-to-understand manner
I tried to explain how to get the article content with MediaWiki API in an easy-to-understand manner with examples (Python 3)
[Deep Learning from scratch] I tried to explain the gradient confirmation in an easy-to-understand manner.
I tried to graph the packages installed in Python
I tried to summarize how to use pandas in python
[Python] I tried to explain words that are difficult for beginners to understand in an easy-to-understand manner.
I tried to summarize the string operations of Python
I tried to display the analysis result of the natural language processing library GiNZA in an easy-to-understand manner
I tried to summarize the code often used in Pandas
I tried to summarize the commands often used in business
I tried to implement the mail sending function in Python
I tried to summarize Python exception handling
I tried to implement PLSA in Python
I tried to summarize the umask command
I tried to implement permutation in Python
I tried to implement PLSA in Python 2
Python3 standard input I tried to summarize
I tried to summarize the graphical modeling.
I tried to implement ADALINE in Python
I tried to implement PPO in Python
I will explain how to use Pandas in an easy-to-understand manner.
I tried to summarize what python strong people are doing in the competition professional neighborhood
[For beginners] I want to explain the number of learning times in an easy-to-understand manner.
I tried to summarize until I quit the bank and became an engineer
I tried to understand supervised learning of machine learning in an easy-to-understand manner even for server engineers 1
Comparing the basic grammar of Python and Go in an easy-to-understand manner
I tried simulating the "birthday paradox" in Python
I tried the least squares method in Python
I tried to implement TOPIC MODEL in Python
I tried to summarize the contents of each package saved by Python pip in one line
I tried to implement selection sort in python
I tried to understand supervised learning of machine learning in an easy-to-understand manner even for server engineers 2
LeetCode I tried to summarize the simple ones
I want to display the progress in Python!
I tried to summarize all the Python plots used in the research by active science graduate students [Basic]
[Python] I want to know the variables in the function when an error occurs!
[Python] I tried to get the type name as a string from the type function
I tried to summarize how to use matplotlib of python
I tried to summarize the basic form of GPLVM
I tried to touch the CSV file with Python
I tried to solve the soma cube with python
I tried to implement a pseudo pachislot in Python
Set the form DateField to type = date in Django
I tried to implement Dragon Quest poker in Python
I tried to implement an artificial perceptron with python
I tried to implement GA (genetic algorithm) in Python
[Python] I tried to graph the top 10 eyeshadow rankings
I want to write in Python! (3) Utilize the mock
I tried to solve the problem with Python Vol.1
I want to use the R dataset in python
Python OpenCV tried to display the image in text.
I tried to summarize SparseMatrix
I tried to summarize all the Python visualization tools used in research by active science graduate students [Application]
I checked the reference speed when using python list, dictionary, and set type in.
I tried to summarize the new coronavirus infected people in Ichikawa City, Chiba Prefecture
[Python] I tried to summarize the array, dictionary generation method, loop method, list comprehension notation
[Machine learning] Let's summarize random forest in an easy-to-understand manner
[First COTOHA API] I tried to summarize the old story
I tried the accuracy of three Stirling's approximations in python