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 frozenzet().
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 set
del myset
2.the oprator of set
2.1 standard operator
in,no in,==,!= , > , >=,
2.2 'set' type operator
combine: "|"
> twoset=se("xyz")
> oneset | twoset
set(['a', 'c', 'b', 'd', 'y', 'x', 'z'])
&, -, ^
>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 not for frozenset
Update:|=, &=, -=, ^=
3.build_in function
len(), set(), frozenset()
3.1 set-type build_in function
Note:build_in function means standart build_in function that could used for set .
set-type build_in function means this function is 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 be set-type, while the object for the build_in function could be iterative type.