The previous section describes the basic concepts of a set. Note that the set involved here is a set that can be modified in the original place. There is also a set that cannot be modified in the original place.
Frozen set
The previous section describes the basic concepts of a set. Note that the set involved here is a set that can be modified in the original place. There is also a set that cannot be modified in the original place. The method for creating this set is:
>>> F_set = frozenset ("qiwsir") # read this name and find frozen, frozen set >>> f_setfrozenset (['Q', 'I ','s ', 'R', 'w'])> f_set.add ("python") # Error Traceback (most recent call last): File"
", Line 1, in
AttributeError: 'frozenset' object has no attribute 'add' >>> a_set = set ("github") # compare and take a look, this is a set> a_setset (['B', 'G', 'I', 'H', 'u ', 't']) >>>> a_set.add ("python") >>a_setset (['B', 'G', 'I', 'H', 'Python ', 'U', 't'])
Set operation
First, let's review some of the collected knowledge in middle school mathematics (accurately speaking, a little knowledge in high school mathematics), mainly to arouse the painful and beautiful memories, at least for me.
Relationship between elements and sets
Whether the element belongs to a set.
>>> asetset(['h', 'o', 'n', 'p', 't', 'y'])>>> "a" in asetFalse>>> "h" in asetTrue
Tangle between set and set
Assume that two sets A and B
Whether A is equal to B, that is, the two sets have identical elements.
Experiment in interactive mode
>>> a set(['q', 'i', 's', 'r', 'w'])>>> bset(['a', 'q', 'i', 'l', 'o'])>>> a == bFalse>>> a != bTrue
Whether A is A subset of B or, in turn, whether B is A superset of. That is, the elements of A are also elements of B, but the elements of B are more than the elements of.
Experiment
>>> Aset (['Q', 'I','s ', 'R', 'w']) >>> cset (['Q ', 'I']) >>> c> c. issubset (a) # or use this method to determine whether c is a subset of a. True>. issuperset (c) # True> bset (['A', 'Q', 'I', 'L ', 'O'])>> A. issubset (B) # or False
The union of A and B, that is, all elements of A and B, as shown in
>>> Aset (['Q', 'I','s ', 'R', 'w']) >>> bset (['A ', 'q', 'I', 'L', 'O'])> a | B # There are two possible methods, with the same result set (['A ', 'I', 'L', 'O', 'Q','s ', 'R', 'w'])>. union (B) set (['A', 'I', 'L', 'O', 'Q', 'S', 'R', 'w'])
The intersection of A and B is the public element of A and B, as shown in
>>> Aset (['Q', 'I','s ', 'R', 'w']) >>> bset (['A ', 'q', 'I', 'L', 'O'])> a & B # is equivalent to set (['Q ', 'I']) >>>. intersection (B) set (['Q', 'I'])
During my experiment, I knocked on the following code. The result is as follows. can you explain it? (Questions)
>>> A and bset (['A', 'Q', 'I', 'L', 'O'])
The difference (supplement) between A and B, that is, the elements of A that are different from B, as shown in
>>> Aset (['Q', 'I','s ', 'R', 'w']) >>> bset (['A ', 'q', 'I', 'L', 'O'])> a-bset (['S', 'R ', 'w'])>. difference (B) set (['S', 'R', 'w'])
-Symmetric difference set of A and B, as shown in
>>> Aset (['Q', 'I','s ', 'R', 'w']) >>> bset (['A ', 'q', 'I', 'L', 'O'])>. symmetric_difference (B) set (['A', 'L', 'O', 'S', 'R', 'w'])
The above is the basic operation of the set. In programming, you can use the method described above to search for it. You do not have to memorize it.