"Learning Python with You Hand in Hand" 24-Collection

In the last article "Learning Python with You" 23-Built-in Sequence Functions , we learned 4 kinds of built-in sequence functions in Python, and at the same time learned how to use sequence functions to build dictionaries.

In this article, we will learn the last data structure in this stage-collection. We are not unfamiliar with the concept of sets. In college mathematics and high school, a little knowledge of sets is involved, such as intersections, unions, etc., which we know are all operations on sets.

Today, we will systematically explain the concept of sets in Python and related methods of operation. At the same time, we will also introduce the data structures (lists, tuples, dictionaries) and scalars (strings, numbers, Boolean values) that we have learned before. ) Wait to be added to today’s example, so that everyone can have an overall understanding and review of what has been learned before.

1. Definition of collection

A set is an unordered sequence of non-repeating elements. You can use the braces {} or set() to create a set. The elements of a collection can be immutable types such as numbers, booleans, strings, tuples, etc., but cannot be variable types such as lists and dictionaries, which means that the elements of the collection must be "hashable" (see "Learning Python with You Hand in Hand" 22- extended content in the dictionary ).

At the same time, the most important characteristics of collections are "disorder" and "non-repetition". Therefore, collections cannot be indexed, and duplicate elements will be "de-duplicated" when they are placed in the collection.

In [1]: {1, 'a', True, (3, 2), 1}   # 使用{}是最容易的定义集合方法,重复的元素会被去掉(True也是1,因此也会被去重),因为无序,顺序也可能会调整
Out[1]: {(3, 2), 1, 'a'}

In [2]: {[1, 2], 1}   # 列表不能作为集合的元素
Out[2]: ---------------------------------------------------------------------------
        TypeError                                 Traceback (most recent call last)
        <ipython-input-14-cb9a458f6989> in <module>
        ----> 1 {[1, 2], 1}   # 列表不能作为集合的元素
        
        TypeError: unhashable type: 'list'

In [3]: {
   
   {1:1}, 1}   # 字典也不能作为集合的元素
Out[3]: ---------------------------------------------------------------------------
        TypeError                                 Traceback (most recent call last)
        <ipython-input-15-97fed40bc698> in <module>
        ----> 1 {
   
   {1:1}, 1}   # 字典也不能作为集合的元素
        
        TypeError: unhashable type: 'dict'

When using the set() function to define a collection, set() will use a traversal method to split the parameters to form a collection. For data types that cannot be traversed, they cannot be directly used as parameters of set(), but need to be used as elements of tuples, and tuples are used as parameters of set().

In [4]: set("Good")   # 字符串被拆分为字母,同时去重
Out[4]: {'G', 'd', 'o'}

In [5]: set(123)   # 数字因为不能被遍历,所以不能够使用set()生成集合
Out[5]: ---------------------------------------------------------------------------
        TypeError                                 Traceback (most recent call last)
        <ipython-input-30-9becc342fd9e> in <module>
        ----> 1 set(123)
        
        TypeError: 'int' object is not iterable

In [6]: set((123,))   # 注意定义只包含一个元素的元组的方法
Out[6]: {123}

Because set() can only have one parameter, when defining a set that includes multiple elements, it must be placed in a tuple. Because lists and dictionaries can be traversed, they can also be used as parameters of set() and add their elements to the set.

In [7]: set(("Good", 123, 123))
Out[7]: {123, 'Good'}

In [8]: set(["Good", 123, 123])
Out[8]: {123, 'Good'}

In [9]: set({1:2,'3':'4'})   # 需要注意的是,当字典作为元素时,只有键会被遍历并添加到集合中
Out[9]: {1, '3'}

When defining an empty set, you can only use the set() function, because {} is a way to define an empty dictionary.

In [10]: print("set()的类型是{},{
   
   {}}的类型是{}".format(type(set()), type({})))   # 注意字符串的格式化输出方法中,输出{}的方法
Out[10]: set()的类型是<class 'set'>,{}的类型是<class 'dict'>

2. The increase of collection elements

There are two ways to add elements to the collection, one is the .add() method and the other is the .update() method.

The .add() method can only have one parameter, and the parameter must be immutable (that is, it can be hashed).

In [11]: set1 = set((1, 2, 3))
         set1.add(4)
         set1
Out[11]: {1, 2, 3, 4}

In [12]: set1.add(4, 5, 6)   # 只能有一个参数
         set1
Out[12]: ---------------------------------------------------------------------------
         TypeError                                 Traceback (most recent call last)
         <ipython-input-6-fc53492f5320> in <module>
         ----> 1 set1.add(4, 5, 6)
               2 set1
        
         TypeError: add() takes exactly one argument (3 given)


In [13]: set1.add((4, 5, 6))   # 元组中包含的多个元素,只作为一个元素(元组)添加到集合中,这是与set()函数的区别
         set1
Out[13]: {(4, 5, 6), 1, 2, 3, 4}

In [14]: set1.add([4, 5, 6])   # 列表、字典因为都是可变的,不能作为.add()的参数
         set1
Out[14]: ---------------------------------------------------------------------------
         TypeError                                 Traceback (most recent call last)
         <ipython-input-9-ed1640e4e4fc> in <module>
         ----> 1 set1.add([4, 5, 6])   # 列表、字典因为都是可变的,不能作为.add()的参数
               2 set1    
        
         TypeError: unhashable type: 'list'

The method of adding elements in the .update() method is completely different from that of .add(). The latter is what element is added to the collection, while the former is to traverse all the elements in the parameter by traversing. Add them to the collection one by one.

Therefore, data types that cannot be traversed, such as numbers and booleans, cannot be used as parameters of the .update() method. While variable elements such as lists and dictionaries cannot be used as elements of the collection, they can be traversed through the .update() method, and their elements are added to the collection, and the .update() method does not limit the number of elements .

In [15]: set2 = set((1, 2, 3))

In [16]: set2.update(4)   # 数字不能遍历,所以报错
         set2
Out[16]: ---------------------------------------------------------------------------
         TypeError                                 Traceback (most recent call last)
         <ipython-input-16-5573d88f6ffa> in <module>
         ----> 1 set2.update(4)   # 数字不能遍历,所以报错
               2 set2
          
         TypeError: 'int' object is not iterable

In [17]: set2.update((4))   # (4)代表4加上小括号的数学运算,所以还是4,报错
         set2
Out[17]: ---------------------------------------------------------------------------
         TypeError                                 Traceback (most recent call last)
         <ipython-input-17-1e5a31168b76> in <module>
         ----> 1 set2.update((4))   # (4)代表4加上小括号的数学运算,所以还是4,报错
               2 set2
        
         TypeError: 'int' object is not iterable

In [18]: set2.update((4,))   # 注意只有一个元素的元组的定义方法
         set2
Out[18]: {1, 2, 3, 4}

In [19]: set2.update((4, 5), "Good", ['a', 'd', True, False], {6:7, '8':'9', 'X':'Y'})   # 请大家试试看能否得到与输出结果一致的答案,注意字典的遍历结果是什么
         set2
Out[19]: {1, 2, 3, 4, 5, 6, '8', False, 'G', 'X', 'a', 'd', 'o'}

3. Deletion of collection elements

There are three methods for deleting collection elements, which are the .remove() method, the .discard() method and the .pop() method. There is also a method to clear the collection.clear().

The .remove() method can directly delete a specific element from the collection. If the element does not exist, an error will be reported.

In [20]: set3 = {1, 2, 3, 'a', 'b', 'c'}

In [21]: set3.remove(3)
         set3
Out[21]: {1, 2, 'a', 'b', 'c'}

In [22]: set3.remove(3)   # 3已经在上一步中被删除了,因此报错
         set3
Out[22]: ---------------------------------------------------------------------------
         KeyError                                  Traceback (most recent call last)
         <ipython-input-27-6cccfd8ba26e> in <module>
         ----> 1 set3.remove(3)   # 3已经在上一步中被删除了,因此报错
               2 set3
          
          
         KeyError: 3

The .discard() method can also directly delete a specific element from the collection, but when the element does not exist, no error will be reported.

In [23]: set3.remove('b')
         set3
Out[23]: {1, 2, 'a', 'c'}

In [24]: set3.discard('b')   # 虽然'b'已经在上一步中被删除的,也不会报错
         set3
Out[24]: {1, 2, 'a', 'c'}

The .pop() method randomly deletes an element from the set and returns the element value of the deleted element. Although it is a random deletion, in interactive mode, the .pop() method deletes the first element on the left after the collection is sorted (although unordered, the collection is generally sorted in ASCII order, from the previous example Can also be seen).

In [25]: set3.pop()   # 排序后左侧第一个元素是1
Out[25]: 1

In [26]: set3
Out[26]: {2, 'a', 'c'}

The .clear() method is to delete all the elements in the collection to form an empty collection.

In [27]: set3.clear()
         set3
Out[27]: set()

4. Set operations

Set operations refer to intersections, unions, etc. that we are more familiar with. In addition, there are differences and symmetric differences that we are not familiar with.

In [28]: set4 = {1, 2, 3, 4, 5}
         set5 = {4, 5, 6, 7, 8, 9}

In [29]: set4.union(set5)   # 并集,或者使用二元操作符(set4 | set5)
Out[29]: {1, 2, 3, 4, 5, 6, 7, 8, 9}

In [30]: set4 | set5   # 其它运算不再列举二元操作符的实例
Out[30]: {1, 2, 3, 4, 5, 6, 7, 8, 9}

In [31]: set4.intersection(set5)   # 交集,或者使用二元操作符(set4 & set5)
Out[31]: {4, 5}

In [32]: set4.difference(set5)   # 差集,即在set4但不在set5的元素,或者使用二元操作符(set4 - set5)
Out[32]: {1, 2, 3}

In [33]: set4.symmetric_difference(set5)   # 对称差集,即在set4和set5但不同时在set4和set5的元素,或者使用二元操作符(set4 ^ set5)
Out[33]: {1, 2, 3, 6, 7, 8, 9}

When the above method is used for calculation, the values ​​of set4 and set5 will not be changed. If you want to change the value of se4 while calculating, you need to use the .update() method, or add the word update to the above method. At the same time, the binary operators have also changed accordingly, just like the a=a+1 and a+=1 we introduced earlier.

In [34]: set4.update(set5)   # 并集并赋值给set4,或者使用二元操作符(set4 |= set5)
         set4
Out[34]: {1, 2, 3, 4, 5, 6, 7, 8, 9}

In [35]: set4 = {1, 2, 3, 4, 5}
         set5 = {4, 5, 6, 7, 8, 9}
         set4.intersection_update(set5)
         set4   # 交集并赋值给set4,或者使用二元操作符(set4 &= set5)
Out[35]: {4, 5}

Other similar methods include a.difference_update(b) (binary operator: a -= b), a. symmetric_difference_update(b) (binary operator: a ^= b), here are not examples.

5. Set judgment

The judgment of a set is mainly to judge whether a set contains correlation operations with another set.

In [36]: set6 = {1, 2, 3, 4, 5}
         set7 = {1, 2, 3}

In [37]: set7.issubset(set6)   # set7是不是set6的子集
Out[37]: True

In [38]: set6.issuperset(set7)   # set6是不是set7的父集
Out[38]: True

In [39]: set8 = {6, 7}
         set6.isdisjoint(set8)   # 如果set6和set8没有交集,返回True
Out[39]: True

In [40]: set9 = {1, 2, 3}
         1 in set9   # 可以使用我们之前学过的in,来判断一个元素是否包含在集合中
Out[40]: True

In [41]: set10 = {1, 2, 3}
         set9 == set10   # 判断两个集合是否相同,这里不能用is,因为内存地址不同
Out[41]: True

6. Set functions

What we have introduced above are all methods of collection. Below we introduce a collection function len(), which is a function to calculate the number of elements in a collection.

In [42]: len({1, 'a', True, (3, 2), 1})
Out[42]: 3

The above is our introduction to Python collections. So far we have introduced the most basic data structures in Python. In the next article, we will introduce the derivation of lists, sets, and dictionaries. This is also one of the most popular language features of Python, so stay tuned.

 

 


Thanks for reading this article! If you have any questions, please leave a message and discuss together ^_^

To read other articles in the "Learning Python with You Hand in Hand" series, please follow the official account and click on the menu selection, or click the link below to go directly.

"Learning Python with You Hand in Hand" 1-Why learn Python?

"Learning Python with you hand in hand" 2-Python installation

"Learning Python with You Hand in Hand" 3-PyCharm installation and configuration

"Learning Python with You Hand in Hand" 4-Hello World!

"Learning Python with You Hand in Hand" 5-Jupyter Notebook

"Learning Python with You Hand in Hand" 6-String Identification

"Learning Python with You Hand in Hand" 7-Index of Strings

"Learning Python with You Hand in Hand" 8-String Slicing

"Learning Python with You Hand in Hand" 9-String Operations

"Learning Python with You Hand in Hand" 10-String Functions

"Learning Python with You Hand in Hand" 11-Formatted Output of Strings

"Learning Python with You Hand in Hand" 12-Numbers

"Learning Python with You Hand in Hand" 13-Operation

"Learning Python with You Hand in Hand" 14-Interactive Input

"Learning Python with You Hand in Hand" 15-judgment statement if

"Learning Python with You Hand in Hand" 16-loop statement while

"Learning Python with You Hand in Hand" 17-the end of the loop

"Learning Python with You Hand in Hand" 18-loop statement for

"Learning Python with You Hand in Hand" 19-Summary of the first stage

"Learning Python with You Hand in Hand" 20-List

"Learning Python with You Hand in Hand" 21-Tuples

"Learning Python with You Hand in Hand" 22-Dictionary

"Learning Python with You Hand in Hand" 23-Built-in Sequence Function

For Fans: Follow the "also said Python" public account, reply "Hand 24", you can download the sample sentences used in this article for free.

Also talk about Python-a learning and sharing area for Python lovers

Guess you like

Origin blog.csdn.net/mnpy2019/article/details/102782511