Data type: Int/str/bool/list/dict/tuple/float/set (set type natural deduplication)
I. Definition of a collection
s = set () #定义空集合
s = {' A ', ' B ', ' C ', ' d '} #集合不是key-value-shaped, no colon
The set is unordered and cannot be evaluated by subscript.
Second, set assignment value
S.add ()
s = {' A ', ' B ', ' C ', ' d '}
S.add (' Ijk ') difference between #注意add and update
# s.update (' FGH ')
Print (s)
Output Result:
{' d ', ' Ijk ', ' C ', ' B ', ' A '}
S.update ()
Output Result:
{' F ', ' B ', ' G ', ' d ', ' a ', ' C ', ' H '}
s = Set ()
s = set (' Cheeseshop ')
Print (s)
Output Result:
{' s ', ' e ', ' P ', ' h ', ' O ', ' C '}
Third, delete the collection element
S.remove ()
s = set (' Cheeseshop ')
S.remove (' er ') # Delete non-existent will error
S.remove (' e ')
Print (s)
s.pop () #随机删除一个
s.discard (' er ') #如果删除的元素存在, delete, do not exist do not handle
del s # Delete Collection
Iv. Collection of common operations
S-= set (' Copy ') equivalent to S = S-set (' copy ')
Take intersection
S.intersection (S1) equivalent to S & S1
Fetch and set
S.union (S1) equivalent to S | S1
Take the difference set
S.difference (S1) is equivalent to s-s1
Summary of common methods for Python collections