Python basic course (6 sets)

set

A set holds a unique group of data. In other words, even if you register the same data, it will be ignored. Enclose the set in {}. Or it can be expressed by set ([]).

set_explain1.py


set_a = {1,2,"a","b",1,"a"}
set_b = set([1,2,"a","b",1,"a"])
print("set_a {0}".format(set_a))
print("set_b {0}".format(set_b))

The output set contains only one 1 and "a". Also, since sets do not have a data order, they cannot be indexed like strings and lists.

Copy the following program and execute it. I will explain each function.

set_explain.py


set_a = {7,9,2}
set_b = {9,22,2,25}

set_b_25 = 25 in set_b
set_b_0 = 0 in set_b
print("set_b_25 {0}".format(set_b_25))
print("set_b_0 {0}".format(set_b_0))

print("len(set_a) {0}".format(len(set_a)))

set_b.add(12)
set_b.remove(25)
set_b.discard(0)
set_c = {"a","b","c"}

set_union = set_a.union(set_b)
print("set_union {0}".format(set_union))

set_unions = set_a.union(set_b,set_c)
print("set_unions {0}".format(set_unions))

set_intersection = set_a.intersection(set_b,set_c)
print("set_intersection {0}".format(set_intersection))

set_difference = set_a.difference(set_b)
print("set_difference {0}".format(set_difference))

set_differences = set_a.difference(set_b,set_c)
print("set_differences {0}".format(set_differences))

set_difference_1 = set_a.difference(set_b)
set_difference_2 = set_b.difference(set_a)
print("set_difference_1 {0}".format(set_difference_1))
print("set_difference_2 {0}".format(set_difference_2))

set_symmetric_difference = set_a.symmetric_difference(set_b)
print("set_symmetric_difference {0}".format(set_symmetric_difference))

set_big = {"a","b","c"}
set_small = {"a","b"}
set_issuper = set_big.issuperset(set_small)
set_issub = set_small.issubset(set_big)
print("set_issuper {0}".format(set_issuper))
print("set_issub {0}".format(set_issub))

Confirmation of the existence of elements in the set

Like a list, a set can also be ** x in set ** to see if an element exists in the set.

len (set)

You can get the number of elements as well as the list. However, unlike the list, max () and min () do not exist in the set. Since indexing is not possible, it is not possible to replace by specifying the range.

Add data

It can be added with ** set.add (element) **. As mentioned earlier, even if you add an element that already exists There can never be more than one of the same element in a set.

Delete data

It can be removed with ** set.remove (element) **. However, using remove () will result in an error if the element does not exist in the set. You can check with ** x in set ** in advance, but there is a convenient command called ** set.discard (element) **. This is a convenient feature that "deletes only when the element exists in the set".

The advantages of using sets are unions (mergers), intersections (intersections), and differences (difference sets). The point is that processing can be performed at high speed.

Union (merger)

** Set 1. union (Set 2, Set 3,…) ** This is the process of removing duplicate elements from multiple sets to create a single set. The same process is possible with 3 or more sets.

Intersection (intersection / intersection)

** Set 1.intersection (Set 2, Set 3,…) ** Extracts elements that are duplicated in both (all) of the specified set. Similar to the union, the same processing can be performed from three or more elements.

Difference

** Set 1.difference (Set 2, Set 3,…) ** Extracts elements that exist on one side and not on the other side. Like the union, it can be processed from 3 or more elements.

Symmetric difference

In a normal difference, in X.difference (Y) and Y.difference (X) The results will be different (unless they are both in the same set). Check the executed program.

set_a set([9, 2, 7]) set_b set([2, 12, 22, 9]) set_difference_1 set([7]) set_difference_2 set([12, 22])

On the other hand, if you use a symmetric difference, you can use it regardless of the order. It is convenient to be able to extract the items that exist in one set of X and Y. ** Set 1.symmetric_difference (Set 2) ** Symmetric differences are limited to two sets.

Superset subset

In this way, sets are useful for determining duplicate values and handling joins.

Next: Python Basic Course (7 Dictionary)

Recommended Posts

Python basic course (6 sets)
Python basic course (12 functions)
Python Basic Course (7 Dictionary)
Python basic course (2 Python installation)
Python Basic Course (11 exceptions)
Python Basic Course (Introduction)
Python basic course (13 classes)
Python basic course (8 branches)
Python Basic Course (3 Python Execution)
Python basic course (10 inclusion notation)
Python Basic Course (1 What is Python)
Python Basic Course (14 Modules and Packages)
Basic Python writing
Python3 basic grammar
RF Python Basic_02
Python Basic Course (at the end of 15)
Python basic course (4 numeric type / character string type)
Python I'm also basic
Python basic grammar / algorithm
[python] class basic methods
Python3 cheat sheet (basic)
Python basic grammar (miscellaneous)
Python basic memorandum part 2
python basic on windows ②
Python basic memo --Part 2
Basic Python command memo
Python basic grammar note (4)
Python basic grammar note (3)
Basic knowledge of Python
Python basic grammar memo
OpenCV basic code (python)
Python basic memo --Part 1
python memorandum super basic
Python basic if statement
Python Basic --Pandas, Numpy-
Basic Python 3 grammar (some Python iterations)
Python application: Pandas Part 1: Basic
Refactoring Learned in Python (Basic)
BASIC authentication with Python bottle
Python basic dict sort order
Python Basic Grammar Memo (Part 1)
Python basic grammar (miscellaneous) Memo (3)
Python basic grammar (miscellaneous) Memo (2)
Basic Python grammar for beginners
[Python] [SQLite3] Operate SQLite with Python (Basic)
Basic usage of Python f-string
I learned Python basic grammar
Python basic grammar (miscellaneous) Memo (4)
Python (Python 3.7.7) installation and basic grammar
Java and Python basic grammar comparison
Python
Scraping with Selenium in Python (Basic)
Python course for data science_useful techniques
[Python] Basic knowledge used in AtCoder
I took Progete's Python Learning Course I
1. Statistics learned with Python 1-1. Basic statistics (Pandas)
Basic grammar of Python3 system (dictionary)
Basic Python operation 2nd: Function (argument)
Python application: data visualization part 1: basic
Basic study of OpenCV with Python
Basic Linear Algebra Learned in Python (Part 1)