If you are confused about the actual application steps of the collection type in the Python dictionary, or are interested in the opposite, you can browse our articles, not only can you solve your problem, it can also stimulate your interest in the Python dictionary computer language.
Set Type
① Set () and frozenset () using the factory method of the set ():
- >>> s = set('cheeseshop')
- >>> s
- set(['c', 'e', 'h', 'o', 'p', 's'])
- >>> t = frozenset('bookshop')
- >>> t
- frozenset(['b', 'h', 'k', 'o', 'p', 's'])
- >>> type(s)
- <type 'set'>
- >>> type(t)
- <type 'frozenset'>
② How to update a set and use the built-in methods and operators of various sets to add and delete members of the Set:
- >>> s.add('z')
- >>> s
- set(['c', 'e', 'h', 'o', 'p', 's', 'z'])
- >>> s.update('pypi')
- >>> s
- set(['c', 'e', 'i', 'h', 'o', 'p', 's', 'y', 'z'])
- >>> s.remove('z')
- >>> s
- set(['c', 'e', 'i', 'h', 'o', 'p', 's', 'y'])
- >>> s -= set('pypi')
- >>> s
- set(['c', 'e', 'h', 'o', 's'])
③ Delete a set
- del s
④ Member relationship (in, not in)
- >>> s = set('cheeseshop')
- >>> t = frozenset('bookshop')
- >>> 'k' in s
- False
- >>> 'k' in t
- True
- >>> 'c' not in t
- True
⑤ Set equivalence/non-equivalence
- >>> s == t
- False
- >>> s != t
- True
- >>> u = frozenset(s)
- >>> s == u
- True
- >>> set('posh') == set('shop')
- True
⑥ Differential or relative complementary set (-) of the difference or relative complementary set of the Two sets (s and t) refers to a set C, the elements in the set, only belongs to the set s, not
In set t. The difference symbol has an equivalent method,
- difference().
- >>> s - t
- set(['c', 'e'])
-
Symmetric Difference (^): symmetric difference is the XOR of the set. The above article describes the practical application steps of the Python dictionary for the set type.