There are two types of set:variable set and immutable set:variable set can not be hashed. Note:frozenset can not be changed!
1.create and update
> Myset=set ("abadsfljasd")
> MySet
set ([' A ', ' B ', ' d ', ' F ', ' j ', ' l ', ' s '])
Note:we can not create a set by operator (can not treated as list ant tuple or diretoty). We can only use Set () ANF Frozenze T ().
1.1visit The value in a set:
' A ' in MySet
1.2update a set
Myset.add (' z ')
myset.update ("python")
myset.remove ("ABCD")
s-=set ("Python")
1.3delete a setDel MySet
2.the Oprator of Set 2.1 Standard operatorIn,no in,==,!=, >, >=, <=
2.2 ' Set ' type operator
Combine: "|"
> Twoset=se ("xyz")
> Oneset | twoset
Set ([' A ', ' C ', ' B ', ' d ', ' Y ', ' x ', ' Z '])
And,-, ^
>oneset ^ threeset
>set ([' C ', ' d ', ' 1 ', ' 3 ', ' 2 '])
Mixed-type Operate:set and frozen set
> Myset=set ("ABCD")
> Myfrozenset=frozenset ("1234")
> MySet | Myfrozenset
Set ([' A ', ' C ', ' B ', ' d ', ' 1 ', ' 3 ', ' 2 ', ' 4 '])
> Myfrozenset | myset
frozenset ([' A ', ' C ', ' B '), ' d ', ' 1 ', ' 3 ', ' 2 ', ' 4 ']
Note:the result type follow the former.
2.3set-type operator---only used for set is not for Frozensetupdate:|=, &=, =, ^=
3.build_in functionLen (), set (), Frozenset ()
3.1 set-type build_in functionnote:build_in function means Standart build_in function that could the for set. Set-type build_in function means this function are part of Set-type.
Function name return value
S.add (' a ') update s
S.clear () Clear S
S.difference (t) a new set (element in S but not in T)
S.union (t) a new set
S.intersection (t) a new set
S.issubset (t) True or False
3.2compare:operator and build_in function: For the most, build_in function and Operarot is equal. However, the operand for the operator must is Set-type, while the object for the Build_in function could is iterative type .