set features: the different elements together, the element hash value must be immutable
Create a set of
SET = S ( 'Alex Li') Print (S) forms; deduplication { 'e', 'i' , '', 'l', 'a', 'x'}
to re-set collection
and = [ 'CX', 'chenxi' 'CX'] print () print (set ()) [ 'CX', 'chenxi' 'CX'] {chenxi '' CX '}
to re-set and view the data type
and = [ 'CX', 'chenxi' 'CX'] print () print (set (and) type (set ())) { 'i', 'a', 'e', 'x' , '', 'l'} 'CX', 'chenxi' 'CX'] {chenxi '' CX '} <class' set ''
set of different combinations of elements together, but the element must not change
li = [[1,2],3,'tyuio'] a = set(li) print(a) Traceback (most recent call last): File "D:/python/map.py", line 308, in <module> a = set(li) TypeError: unhashable type: 'list'
set collection is divided into a set of variable and non-variable collection; creation mode
s1 = set('alvin') # 可变 s2 = frozenset('yuan') #不可变 print(s1,type(s1)) print(s2,type(s2)) 测试 {'i', 'v', 'l', 'a', 'n'} <class 'set'> frozenset({'n', 'y', 'u', 'a'}) <class 'frozenset'>
Access to the collection of collections to determine whether there exists an element
S1 = SET ( 'Alvin') Print ( 'a' in S1) # determines a element exists in the collection; presence of Ture, absent False Print ( 'B' in S1) for I in S1: Print (I) true False A L n- I V
The collection add a single element
def df (): # view the content of the element function # print ( 'a' in s1 ) # determines a element exists in the collection; presence of Ture, False absence # Print ( 'B' in S1) Print ( "* ******************************* ") for I in S1: Print (I) Print ( '\ n---- ------ \ n-') a = [1,5,2, "wertayuio", 6,5] S1 = SET (a) DF () s1.add ( "WSD") added to the element in a # element DF () False False ******************************** . 1 2 wertayuio . 5 . 6 ------- - False False ******************************** . 1 wertayuio . 5 . 6 ---------
Update (add more elements) elements
s1.update ( "ops2") # additive element as a sequence, if repeated, add only one DF () . 1 2 . 5 . 6 wertayuio --------- . 1 2 . 5 . 6 P wertayuio O S 2 ---- -----
Update (add more elements) elements
s1.update ([12, 'wwww' ]) # If the list update only add two DF () Test D: \ pyth \ python.exe D: /python/map.py wertayuio . 1 2 . 5 . 6 ----- ---- wertayuio . 1 2 . 5 . 6 12 is of wwww ---------
Delete the specified set of operating elements
s1.remove (2) # delete the specified operation element DF () . 1 2 . 5 . 6 wertayuio --------- . 1 . 5 . 6 wertayuio ---------
Random deletion
Print (S1) s1.pop () Print (S1) testing {. 1, 2,. 5,. 6, 'wertayuio'} {2,. 5,. 6, 'wertayuio'}
Empty the contents of the collection
print(s1) s1.clear() #清空 print(s1) {1, 2, 'wertayuio', 5, 6} set()
Determining whether a set of two identical
SET = S ( "Chenxi") S1 = SET ( "chenxixi") Print (S == S1) # determines whether the two sets are equal, equal to True True
Determining whether the set is a set comprising 2
print (set ( "qwert") <set ( "qwertyuiop")) # determines which collection contains former True
Collection and with or
print (set ( "12345") or set ( "567890")) # print set The previous set of print (set ( "12345") and set ( "567890")) # print set behind { '2', '4 ',' 5 ',' 1 ',' 3 '} {' 8 ',' 0 ',' 5 ',' 6 ',' 9 ',' 7 '}
Intersection of the sets with union
SET = S1 ( "123456") S2 = SET ( "456789") Print (s2.intersection (S1)) # intersected; two set of elements common to print (s2.union (s1)) # fetch and set, indicating take the set of all elements and two deduplication
print (s1 | s2) and find another method set #
{ '5', '4', '6'}
{'7', '8', '5', '3', '2', '4', '1', '9', '6'}
{'7', '8', '5', '3', '2', '4', '1', '9', '6'}
Taking the difference-set of Assembly
SET = s1 ( "123456") s2 = SET ( "456789") Print (s2.difference (s1)) # s2 taken in some, s1 there are no elements in print (s1.difference (s2)) # s1 taken in some, s2 there are no elements
print (s1.symmetric_difference (s2)) # take the symmetric difference
{'7', '8', '9'}
{'1', '3', '2'}
{'7', '1', '9', '2', '8', '3'}