One: Basic use
1 Uses: relational operation, de-weight
2 defined by: {} separate multiple elements with commas, each element must be immutable (hash) type
Emphasize:
2.1 Elements within a set must be immutable (hash-ready) types
2.2 Elements in a set are unordered
2.3 Elements within a set cannot be duplicated
1s={1,2,'a'}#S=set ({, ' A '}) # Definition of the collection2 Print(type (s))3 4s={1.1,1,'AA', (for all), {'a': 1}}#typeerror:unhashable type: ' Dict '5 6S={1,'a','Hello', (4),}7 forIteminchS:8 Print(item)9 Ten #The collection has a de-weight effect. Ones={1,1,1,1,1,1,1,1,1,'a','b','a'} As={(All-in-all),'a','b','a'} - Print(s) - theS=set ('Hello') - Print(s)examples Show
11, the target of the de-weight must contain values that are immutable types22, the results of the heavy weight will upset the original order.3names=['ASB','ASB','ASB','WSB','WSB','EGON_NB', [A]]4s=set (names)5 6names=list (s)7 Print(names)
The only thing that needs attention is the simple use of the set to weigh .
3 common operations + built-in methods
Priority operations:
1 pythoners={' King Cannon ',' li er ya ',' Chen Duxiu ',' Alex ','wxx',' the love of the "}2print(len (pythoners))
1, Length len
1 Print('li er ya' inchpythoners)2 3pythoners={'King Cannon','li er ya','Chen Duxiu','Alex','wxx','The Brotherhood of the'}4linuxers={'Chen Duxiu','wxx','WLC','Zhangquan Egg'}2, member operations in and not in
1 Print (Pythoners | linuxers) 2 Print (Pythoners.union (Linuxers))
3, | and set
1 Print (Pythoners & linuxers) 2 Print (Pythoners.intersection (linuxers)) 3 Print (Linuxers & Pythoners)
4. & Intersection
1 Print (Pythoners- linuxers) 2 Print (Pythoners.difference (linuxers)) 3 Print (Linuxers- pythoners) 4 Print (Linuxers.difference (Pythoners))
5.-Difference Set
1 Print (pythoners ^ linuxers) 2 Print (Pythoners.symmetric_difference (linuxers)) 3 4 Print (linuxers ^ pythoners)
6. ^ Symmetric difference set
1 s1={1,2,3}2 s2={1,2,3}3print(S1 = = s2)
7, = =
1s1={1,2,3,4,5}2s2={1,2,3}3 Print(S1 > S2)#S1 contains S24 Print(S1.issuperset (S2))5 Print(S2.issubset (S1))6 7s3={1,2,10}8 Print(S1 >S3)9 Tens1={1,2,3,4,5} Ones2={1,2,3,4,5} A Print(S1 >= S2)8. Parent set (including relationship): >,>=
II: Summary of the type
1 Save a value or save multiple values
Multiple values can be stored, and the values must be immutable types
2 ordered or unordered
Disordered
3 variable or not variable
Set set is a mutable type
Python Basic Syntax---collection type of data type