1. Definition of a collection
>>> s = set ([' as ', ' Yu ', 1, 2, ' 1 ']) >>> s{1, 2, ' as ', ' Yu ', ' 1 '}>>> s = set (' Long ') >> > s{' n ', ' ', ' a ', ' Y ', ' l ', ' w ', ' g ', ' O '}
2. The collection is unordered, there is no coincident element
>>> s = Set ([1, 2, 1, 2]) >>> s{1, 2}
3. Collection Additions
>>> s{1, 2}>>> s.add (' Sam ') #添加一项 >>> s{' Sam ', 1, 2}>>> s0 = set ([' HH ', ' King ']) >& Gt;> s.update (S0) # Add multiple >>> s{1, 2, ' King ', ' Sam ', ' hh '}
4. Element deletion
>>> s{1, 2, ' King ', ' Sam ', ' hh '}>>> s.remove (' king ') >>> S{1, 2, ' Sam ', ' hh '}
5. Length
>>> s{1, 2, ' Sam ', ' hh '}>>> Len (s) 4
6. Belonging to
>>> s{1, 2, ' Sam ', ' hh '}>>> ' Sam ' in strue>>> ' Jey ' not in strue>>> ' Jey ' in Sfalse
7. Include
>>> a = set ([' AA ', ' BB ', ' cc ') >>> B = set ([' BB ', ' cc ']) >>> B.issubset (a) # B <= A true& Gt;>> A.issuperset (B) # A >= btrue
8. and set
>>> A = set ([' FF ', ' Yu ']) >>> B = set ([' KK ', ' oo ']) >>> a.union (b) {' Yu ', ' ff ', ' oo ', ' KK '}>> ;> A | b{' Yu ', ' ff ', ' oo ', ' KK '}
9. Intersection
>>> B = set ([' KK ', ' oo ']) >>> a = set ([' FF ', ' KK ']) >>> a.intersection (b) {' KK '}>>> A &A mp b{' KK '}
10. Difference Set
>>> A = set ([' FF ', ' KK ']) >>> B = set ([' KK ', ' oo ']) >>> a.difference (b) {' FF '}>>> a-b{' FF '}
11. Symmetric difference Set
>>> A = set ([' FF ', ' KK ']) >>> B = set ([' KK ', ' oo ']) >>> a.symmetric_difference (b) {' FF ', ' OO '}& gt;>> A ^ b{' ff ', ' OO '}
12. Copying
>>> A = set ([' FF ', ' KK ']) >>> B = A.copy () # Shallow copy >>> b{' ff ', ' KK '}
python--Collection