[Python] [Supplement] Chapter 04-08 Various data structures (creating and manipulating sets)

[Python] [Supplement] Chapter 04-08 Creating and manipulating sets

This section deals with sets. Sets are also an area of data structure. However, this section is explained as a supplementary matter, so you can omit the set.

However, for those who are thinking of taking the Fundamental Information Technology Engineer Examination, the questions will be given in set theory and set calculation. You should also take a look at those who choose Python for afternoon problems.

Please note that ** "set" ** may be ** "set" ** depending on the book.

Creating a set

Now let's create a set. Enter the following code in the ** Python console **.

>>> S = {1, 2, 3, 4}
>>> S
{1, 2, 3, 4}

The set is expressed by enclosing it in ** {} **. In the dictionary, the keys and values are described in ** {} ** separated by ** ":" ** (colon), but when the colon disappears, it becomes a set.

Add elements of the set

Now let's add an element to the set. For sets, use the ** add ** method instead of append. Enter the following code in the ** Python console **. Display the contents of the variable ** S ** once and then execute.

>>> S
{1, 2, 3, 4}
>>> S.add(5)
>>> S
{1, 2, 3, 4, 5}

You should have confirmed that the element was added at the end of the set.

Now, in this state, execute ** add (5) ** again to output.

>>> S.add(5)
>>> S
{1, 2, 3, 4, 5}

As you can see from the results, sets allow you to create unique elements.

Delete elements of the set

To remove it from the elements of the set, use the ** remove method **. Enter the following code in the ** Python console **. Display the contents of the variable ** S ** once and then execute.

>>> S
{1, 2, 3, 4, 5}
>>> S.remove(2)
>>> S
{1, 3, 4, 5}

You can directly specify and delete the elements of the set.

To delete all the elements of the set, use ** clear method **.

>>> S.clear()
>>> S
set()

If you check the contents of S, ** set () ** will be output. This ** set () ** means an empty set.

Search for elements of the set

You can also check if the set contains the specified element. Use the ** in operator ** with lists and dictionaries.

>>> S = {1, 2, 3, 4}
>>> S
{1, 2, 3, 4}
>>> 2 in S
True
>>> 10 in S
False

Finally

This time I touched on the set. You just want to know that the set stores the elements without duplication. Next time, I will touch on set operations and actually implement them on Python.

Return to [Table of Contents Link]

Recommended Posts

[Python] [Supplement] Chapter 04-08 Various data structures (creating and manipulating sets)
[Python] [Supplement] Chapter 04-09 Various data structures (set theory and operations in sets)
[Python] Chapter 04-06 Various data structures (creating dictionaries)
[Python] Chapter 04-05 Various data structures (tuple creation and features)
[Python] Chapter 04-01 Various data structures (list creation and element retrieval)
[Python] Chapter 04-03 Various data structures (multidimensional list)
[Python] Chapter 04-04 Various data structures (see list)
[Python] Chapter 04-02 Various data structures (list manipulation)
[Python] Chapter 04-07 Various data structures (dictionary manipulation)
[Python for Hikari] Chapter 09-02 Classes (Creating and instantiating classes)
Python for Data Analysis Chapter 4
Python for Data Analysis Chapter 2
[Python for Hikari-] <Supplement> Chapter 06-05 Functions (arguments and return values 4)
Python for Data Analysis Chapter 3
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part1-
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part2-
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part4-
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part3-
Memo "Chapter 5 – Dictionaries and Structuring Data"
Python data structures learned with chemoinformatics
Hashing data in R and Python
[Python] Chapter 03-01 turtle graphics (creating a turtle)
Data pipeline construction with Python and Luigi
Python Application: Data Visualization Part 3: Various Graphs
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.1-8.2.5)
Ant book in python: Sec. 2-4, data structures
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.3-8.3.6.1)
Python data structure and internal implementation ~ List ~
[Introduction to Python3 Day 19] Chapter 8 Data Destinations (8.4-8.5)
[Introduction to Python3 Day 18] Chapter 8 Data Destinations (8.3.6.2 to 8.3.6.3)
Python data structure and operation (Python learning memo ③)
[Python] Various data processing using Numpy arrays
Easily graph data in shell and Python
Compress python data and write to sqlite
[Python] Various combinations of strings and values
Creating Google Spreadsheet using Python / Google Data API
Reading OpenFOAM time series data and sets data
Exchange encrypted data between Python and C #