First, the definition set
1.1 Definitions: {} in the plurality of elements separated by commas, immutable data element type, and arrangement of the elements are unordered.
List_1 = >>> [ ' Egon ' , ' MALE ' , 18 is , [ ' Play ' , ' Read ' ]] >>> S = SET (list_1) Traceback (MOST Recent Last Call): File " <pyshell. 1 #> " , Line . 1 , in <Module1> S = sET (list_1) TypeError: unhashable type: ' List ' >>> element must be prompted to set a hash, i.e., immutable type
1.2 are unordered collection of elements, and is not repeated
>>> list1 = [1,1,2,2,3,3] >>> s = set(list1) >>> print(s) {1, 2, 3} >>>
Note: s = {} default dictionary is empty
1.3 Type Conversion
Collection can be converted into iterables binding, but if the object is a container type may iteration, then the child element iterables must be immutable data type
1.4 built-in method
(1) on the intersection s1 & s2
friends1 = { " ZERO " , " Kevin " , " Jason " , " Egon " } # friends are user 1 friends2 = { " of Jy " , " Ricky " , " Jason " , " Egon " } # user 2 friends are = & friends1 RES friends2 RES = friends1.intersection (friends2) Print (RES) { ' Egon ', 'jason'}
(2) taken and set s1 | s2
friends1 = { " ZERO " , " Kevin " , " Jason " , " Egon " } # friends are user 1 friends2 = { " of Jy " , " Ricky " , " Jason " , " Egon " } # user 2 friends are = friends1 RES | friends2 RES = friends1.union (friends2) Print (RES) { ' Egon ' ,'kevin', 'ricky', 'Jy', 'zero', 'jason'}
(3) taking the difference set s1 - s2 or s1.difference (s2)
Take f1 unique friend
friends1 = { " ZERO " , " Kevin " , " Jason " , " Egon " } # friends are user 1 friends2 = { " of Jy " , " Ricky " , " Jason " , " Egon " } # user 2 friends are = friends1 RES - friends2 RES = friends1.difference (friends2) Print (RES) { ' Kevin ', 'zero'}
Take f2 unique friend
friends1 = { " ZERO " , " Kevin " , " Jason " , " Egon " } # friends are user 1 friends2 = { " of Jy " , " Ricky " , " Jason " , " Egon " } # user 2 friends are = friends2 RES - friends1 RES = friends1.symmetric_difference (friends2) Print (RES) { ' Ricky ', ' You ' }
(4) symmetric difference f1 ^ f2
friends1 = { " ZERO " , " Kevin " , " Jason " , " Egon " } # friends are user 1 friends2 = { " of Jy " , " Ricky " , " Jason " , " Egon " } # user 2 friends are ^ = friends2 RES friends1 Print (RES) { ' Kevin ' , ' of Jy ' ,'ricky', 'zero'}
(5) a parent subset (comprising Relations)
l, 2,3 = {S1 } S2 = {1, 2,4 } # no inclusion relationship, the following are comparative False Print (s1.issuperset (S2)) Print (S1> S2) Print (S1 <S2)
l, 2,3 = {s1 } s2 = {1,2 } Print (s1.issubset (s2)) Print (s1> s2) # When s1 is greater than or equal to s2, s1 is to say the father s2
l, 2,3 = {S1 } s2 = {l, 2,3 } Print (S1 == s2) # S1 and s2 each other Sons
1.4 deduplication
Note: 1, only for immutable types deduplication
2, can not guarantee that the original order
l = [1,'a','b',1,1,2,3] l = list(set(l)) print(l) [1, 2, 3, 'a', 'b']
1.5 Other built-in method
(1) discard
l, 2,3 = {S } s.discard ( . 4) # delete the element does not exist, do Nothing Print (S)
remove
l, 2,3 = {S } s.remove ( . 4) # remove elements does not exist, an error Print (S)
pop
>>> s = {1,2,3} >>> res=s.pop() >>> print(res) 1 >>>
(2)update
S = {l, 2,3 >>> } >>> s.update ([4,5,6 ]) >>> Print (S) { . 1, 2,. 3,. 4,. 5,. 6 } >>> s.update ([. 7, [8, 9 are ]]) Traceback (MOST Recent Last Call): File " <pyshell # 16> " , Line. 1, in <Module1> s.update ([ . 7, [8, 9 are ] ]) TypeError: unhashable type: ' List ' >>> child element of the target object must be a hash of data types (i.e., immutable data type)
(3)add
s = {1,2,3} s.add(4) print(res)
Other methods to understand