Frozen collection
The previous section describes the basic concepts of collections, and note that the collections involved are all in situ modified collections. There is also a collection that cannot be modified in situ. This collection is created in the following ways:
>>> F_set = Frozenset ("Qiwsir") #看这个名字就知道了frozen, frozen set
>>> f_set
frozenset (' Q ', ' I ' , ' s ', ' R ', ' W ']
>>> f_set.add ("python") #报错
Traceback (most recent call last):
File "< Stdin> ", line 1, in <module>
attributeerror: ' Frozenset ' object has no attribute ' add '
>>> a_s ET = set ("GitHub") #对比看一看, which is a set >>> A_set set that can be modified in situ
([' B ', ' g ', ' I ', ' h ', ' ' I ', ' t ', ', ', ' ', ']]
>>> a_set.add ("python")
>>> a_set
set ([' B ', ' g ', ' I ', ' h ', ' Python ', ' u ', ' t '])
Set operations
A little bit of knowledge about a set in middle school mathematics (accurately speaking a little knowledge of high school mathematics) is mainly to evoke the painful and sentimental beauty of memories, at least for me.
The relationship between elements and collections
Whether the element belongs to a collection.
>>> aset
Set ([' H ', ' o ', ' n ', ' P ', ', ', ', ', ', ' t ', ' y '])
>>> "A" in Aset
False
>>> "H" in a Set
True
The tangle of sets and sets
Suppose two sets a, B
A is equal to B, that is, the elements of two sets are exactly the same
Experiment in interactive mode
>>> a
set ([' Q ', ' I ', ' s ', ' R ', ' W '])
>>> b
Set ([' A ', ' Q ', ' I ', ' l ', ' o '])
>>& Gt A = = b
False
>>> a!= b
True
A is a subset of B, or, conversely, B is a superset of a. That is, the elements of A are also elements of B, but the elements of B are more than the number of elements of a.
Experiment
>>> a
set ([' Q ', ' I ', ' s ', ' R ', ' W '])
>>> C
Set ([' Q ', ' I '])
>>> c<a< c22/> #c是a的子集
true
>>> C.issubset (a) #或者用这种方法 to determine if C is a subset of
true
>>> A.issuperset (c) #判断a是否是c的超集
True
>>> b
Set ([' A ', ' Q ', ' I ', ' l ', ' o '])
>>> a< b #a不是b的子集
false
>>> A.issubset (b) #或者这样做
False
The set of a, B, all elements A and B, as shown in the following figure
>>> a
set ([' Q ', ' I ', ' s ', ' R ', ' W '])
>>> b
Set ([' A ', ' Q ', ' I ', ' l ', ', ', ', ', ', ', ', ')]
>>> A | b #可以有两种方式, the result is the same as
set ([' A ', ' I ', ' l ', ' o ', ' Q ', ' s ', ' R ', ' W '])
>>> a.union (b)
set ([' A ', ' I ', ' l ') , ' o ', ' Q ', ' s ', ' R ', ' W ']
The intersection of A and B, which is the public element of A and B, as shown in the following figure
>>> a
set ([' Q ', ' I ', ' s ', ' R ', ' W '])
>>> b
Set ([' A ', ' Q ', ' I ', ' l ', ', ', ', ', ', ', ', ')]
>> > A & B #两种方式, equivalence
set ([' Q ', ' I '])
>>> a.intersection (b)
set ([' Q ', ' I '])
I was in the experiment, I knocked the following code, the results are as follows, reader can explain? Study Questions
>>> A and B
set ([' A ', ' Q ', ' I ', ' l ', ' o '])
A relative B's difference (complement), that is, a relative B different parts of the elements, as shown in the following figure
>>> a
set ([' Q ', ' I ', ' s ', ' R ', ' W '])
>>> b
Set ([' A ', ' Q ', ' I ', ' l ', ', ', ', ', ', ', ', ')]
>>> A-b
Set ([' s ', ' R ', ' W '])
>>> a.difference (b)
set ([' s ', ' R ', ' W '])
-The symmetric difference set of a and B, as shown in the following figure
>>> a
set ([' Q ', ' I ', ' s ', ' R ', ' W '])
>>> b
Set ([' A ', ' Q ', ' I ', ' l ', ', ', ', ', ', ', ', ')]
>> > a.symmetric_difference (b)
set ([' A ', ' l ', ' o ', ' s ', ' R ', ' W '])
These are the basic operations of the collection. In programming, if you use it, you can find it by using the method you said earlier. No rote.