1
2
3
4
5
6
7
8
9
10
11
|
>>> a = range(
10
)
>>> a
[
0
,
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
]
>>> a.insert(
3
,
8
)
>>> a.insert(
3
,
6
)
>>> a
[
0
,
1
,
2
,
6
,
8
,
3
,
4
,
5
,
6
,
7
,
8
,
9
]
>>>
set
(a)
set
([
0
,
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
])
>>>
set
(
'boy'
)
set
([
'y'
,
'b'
,
'o'
])
|
1
2
3
4
5
6
|
>>> a =
set
(
'boy'
)
>>> a
set
([
'y'
,
'b'
,
'o'
])
>>> a.add(
'xpleaf'
)
>>> a
set
([
'y'
,
'b'
,
'xpleaf'
,
'o'
])
|
1
2
3
4
5
6
|
>>> b =
set
(
'Hello'
)
>>> b
set
([
'H'
,
'e'
,
'l'
,
'o'
])
>>> b.add((
'xpleaf'
,
'CL'
))
>>> b
set
([
'H'
, (
'xpleaf'
,
'CL'
),
'e'
,
'l'
,
'o'
])
|
1
2
3
4
5
6
7
8
|
>>> a
set
([
'y'
,
'b'
,
'xpleaf'
,
'o'
])
>>> a.update(
'Python'
)
>>> a
set
([
'b'
,
'h'
,
'xpleaf'
,
'n'
,
'P'
,
'o'
,
'y'
,
't'
])
>>> a.update([
1
,
2
,
3
])
>>> a
set
([
1
,
2
,
'b'
,
'h'
,
'xpleaf'
,
'n'
,
'P'
,
3
,
'o'
,
'y'
,
't'
])
|
1
2
3
4
5
6
7
8
9
|
>>> a
set
([
1
,
2
,
'b'
,
'h'
,
'xpleaf'
,
'n'
,
'P'
,
3
,
'o'
,
'y'
,
't'
])
>>> a.remove(
'b'
)
>>> a
set
([
1
,
2
,
'h'
,
'xpleaf'
,
'n'
,
'P'
,
3
,
'o'
,
'y'
,
't'
])
>>> a.remove(
'KeyError'
)
Traceback (most recent call last):
File
"<stdin>"
, line
1
,
in
<module>
KeyError:
'KeyError'
|
1
2
3
4
5
6
7
8
9
|
>>> a
set
([
1
,
2
,
3
,
'h'
,
'xpleaf'
,
'n'
,
'P'
,
'o'
,
'y'
,
't'
])
>>>
import
tab
>>> a.discard(
'xpleaf'
)
>>> a
set
([
1
,
2
,
3
,
'h'
,
'n'
,
'P'
,
'o'
,
'y'
,
't'
])
>>> a.discard(
'CL'
)
>>> a
set
([
1
,
2
,
3
,
'h'
,
'n'
,
'P'
,
'o'
,
'y'
,
't'
])
|
1
2
3
4
5
6
7
8
9
10
|
>>> a.pop()
1
>>> a
set
([
2
,
3
,
'h'
,
'n'
,
'P'
,
'o'
,
'y'
,
't'
])
>>> a.pop()
2
>>> a.pop()
3
>>> a
set
([
'h'
,
'n'
,
'P'
,
'o'
,
'y'
,
't'
])
|
数学符号 | Python符号 |
含义 |
-或\ | - | 差集,相对补集 |
∩ |
& |
交集 |
∪ |
| |
并集 |
取∩的补 | ^ | 对称差集 |
≠ |
!= |
|
= | == | 等于 |
∈ |
in |
属于 |
not in |
不属于 |
1
2
3
4
5
6
7
8
9
10
|
>>> a =
set
(range(
10
))
>>> b =
set
(range(
15
))
>>> a
set
([
0
,
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
])
>>> b
set
([
0
,
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
,
11
,
12
,
13
,
14
])
>>> b - a
set
([
10
,
11
,
12
,
13
,
14
])
>>> a - b
set
([])
|
1
2
|
>>> a & b
set
([
0
,
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
])
|
1
2
|
>>> a.intersection(b)
set
([
0
,
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
])
|
1
2
|
>>> a | b
set
([
0
,
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
,
11
,
12
,
13
,
14
])
|
1
2
|
>>> a.union(b)
set
([
0
,
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
,
11
,
12
,
13
,
14
])
|
1
2
3
4
5
|
set
([
0
,
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
])
>>> b
set
([
0
,
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
,
11
,
12
,
13
,
14
])
>>> a ^ b
set
([
10
,
11
,
12
,
13
,
14
])
|
1
2
3
4
|
>>> a.symmetric_difference(b)
set
([
10
,
11
,
12
,
13
,
14
])
>>> b.symmetric_difference(a)
set
([
10
,
11
,
12
,
13
,
14
])
|
1
2
3
4
|
>>> a == b
False
>>> a != b
True
|
1
2
3
4
5
6
7
8
9
10
11
12
|
>>> a
set
([
0
,
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
])
>>> b
set
([
0
,
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
,
11
,
12
,
13
,
14
])
>>> a
in
b
False
>>> b
in
a
False
>>> a not
in
b
True
>>>
0
in
a
True
|
1
2
3
4
|
>>> a >= b
False
>>> a <= b
True
|
1
2
3
4
|
>>> a <= b
True
>>> a.issubset(b) ===>a是b的子集
True
|
1
2
3
4
5
6
|
>>> a >= b
False
>>> a.issuperset(b) ===>a不是b的超集,即a不包含b
False
>>> b.issuperset(a) ===>b是a的超集,即b包含a
True
|
1
2
3
4
|
>>> len(a)
10
>>> len(b)
15
|
1
2
3
4
5
6
7
8
|
>>> a
set
([
0
,
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
])
>>> b
set
([
0
,
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
,
11
,
12
,
13
,
14
])
>>> hash(a)
Traceback (most recent call last):
File
"<stdin>"
, line
1
,
in
<module>
TypeError: unhashable type:
'set'
|
1
2
|
>>> hash(
'CL'
)
8576051523077447
|
1
2
3
4
5
|
>>>
import
hashlib
>>> a =
'hello'
>>> a = hashlib.md5()
>>> a.hexdigest()
'd41d8cd98f00b204e9800998ecf8427e'
|