A collection .set
set is a collection of basic data type python. shoots as usual is not very common. set of elements is not repeated. disordered. there must be an element of hash (int, str, tuple, bool), we can to remember. set of data is the type dict without saving value, save only the key. set also expressed by {}
Note: set collection element must be a hash, but this set is not chest upwards available hash set is variable. .
1. to remove duplicate set, and unordered
lst = [ " Lily " , ' Lily ' , ' Li Li ' , " Lily " ] lst = List ( SET (lst)) # SET to convert lst, conversion to List Print (lst) # result is: [ ' Li Li ' , ' Lily ' , ' Lili ' ]
2. set for CRUD
1, by
= {S " Jie ' , ' Jie ' , ' LUN ' } s.add ( " Zhou " ) # s.update ([ ' Liu ' ]) was added iteration # # s.update ([ " Marilyn " , " Maryland " , " Maryland " ]) Print (S)
2, delete
S = { " Manila " , " Marilyn " , " Maryland " } Item = s.pop () # delete a random Print (Item) s.remove ( " Marilyn " ) Delete # specified Print (S) s.clear () # direct empty set () the contents of print (s)
3, change
Data collection #set no index, there is no way the positioning, it is not directly modify # can be used to delete, after the addition of the modified embodiment S = { " Manila " , " Marilyn " , " Maryland " } s.remove ( " Manila " ) s.add ( " Muggles friends " ) Print (S)
4, check
#set is an iterative set, may be for loop for SS in S: Print (SS)
3. Common Operations
# Intersection S1 = { " Lily " , ' Lily ' , ' Li Li ' } S2 = { " Ann " , " Lily ' , ' Uu ' } Print (S1 & S2) Print (s1.intersection (S2))
# Union Print (s1 | s2) Print (s1.union (s2))
#差集 print(s1 - s2) print(s1.difference(s2))
# Anti intersection Print (s1 ^ s2) Print (s1.symmetric_difference (s2))
# Subset Print (s1 < s2) # SET1 is a subset of set2 do? print (s1.issubset (s2))
# Superset Print (s1 > s2) # SET1 is a superset of set2 do? Print (s1.issuperset (s2))
# Frozenset is immutable is a hash of the data type. S = frozenset ([ " Lily " , ' Lily ' , ' Li Li ' ]) DIC = {S: 123 } Print (DIC)
II. Copy depth
1. shallow copy. Only copy of the first layer. The second layer does not copy the content. So called shallow copy
# Shallow copy lst1 = [ " Nanshan South " , " Lake Baikal " , " Southern Girl " , " Chengdu " ] # lst2 = lst1 [:] # is also a shallow copy lst2 = lst1.copy () # lst2 and lst1 is not an object the lst1.append ( " Confessions balloon " ) Print (LST1, lst2)
2. a deep copy. The elements within the element completely duplicated copies. ⼀ alterations will not yield ⽣ Another problem of change with
# Deep copy Import Copy lst1 = [ " Nanshan South " , " Lake Baikal " , [ " Southern Girl " , " Chengdu " ]] lst2 = copy.deepcopy (lst1) # lst1 thrown into the deep copy, including all internal SUMMARY copy lst2 [ 2 ] .append ( " advertising balloon " ) Print (LST1, lst2)
III. The underlying data type supplement
#jion list becomes a string li = [ " Li Ka-shing " , " twist vine " , " ⻩ peak Yellow Sea " , " Carina " ] S = " _ " .join (li) Print (S)