When Python executes a class, it is equivalent to executing the __init__ method, for example: List () List __init__
Set is a set of unordered and non-repeating elements that can be considered a set in mathematics
Usage: 1. Create collection S = Set () Create an empty collection S = Set ([11,22,33]) s = set (' asdfghh ') s = {' ASD ', ' SAF '}2. Access collection A in S3. Update collection S.add ()---added, each Only one element at a time can be added s.clear ()----Empty s.upadte ()---Update the collection and assign a value to S. You can iterate through bulk additions, which is equivalent to executing a for loop and then repeating the. Add method. S.discard ()---Delete the specified element, there is no error s.remove ()---Delete the specified element, no error s.pop ()-----Random Delete element
4. Union (|)
Union (Union) operations are actually equivalent to the or operation of a set, and the Union symbol has an equivalent method, Union ().
>>> s1=set (' begin ') >>> s2=set (' man ') >>> s3=s1|s2>>> s3set ([' A ', ' B ', ' e ', ' g ', ' I ') , ' m ', ' n ')
5. Intersection (&)
Equivalent to set and, the equivalent method of the intersection symbol is intersection ()
>>> S1&s2set ([' n ']) >>> s1.intersection (S2) Set ([' N '])
6. Check and fill (-)
The equivalent method is Difference ()
>>> s1-s2 # S1-(S1 & S2) Set ([' I ', ' B ', ' e ', ' G ']) >>> s1.difference (S2) Set ([' I ', ' B ', ' e ', ' G '] )
7. Symmetric difference (^)
The symmetric difference is the XOR (' XOR ') of the set, and the obtained elements belong to the S1,S2 but do not belong to both S1 and S2. Its equivalent method symmetric_difference ()
>>> s1^s2 # (S1 U S2)-(S1 & S2) Set ([' A ', ' B ', ' e ', ' g ', ' I ', ' M ']) >>> s1.symmetric_ Difference (S2) Set ([' A ', ' B ', ' e ', ' g ', ' I ', ' m '])
Note: And,or between collections
>>> S1 and S2 equivalent to S2set ([' A ', ' m ', ' n ']) #取 s2>>> S1 or S2 equivalent to S1set ([' I ', ' B ', ' e ', ' g ', ' n ') ) #取 s1>>>
V. Conversions between collections, lists, tuples, and strings
>>> list (S1) [' I ', ' B ', ' e ', ' g ', ' N ']>>> str (S1) ' Set ([' I ', ' B ', ' e ', ' g ', ' n ']) ">>> tuple (S1) ( ' I ', ' B ', ' e ', ' g ', ' n ')
Reference address forgot, sorry **************************************************.
Python--Set