python集合set

python集合set练习

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# set集合和列表很相似
# 不同点: 无序、只存不可变集合、不能有重复元素
a = {2, 1, 3, 4, 4, 4, 5}
print(a, type(a))

# 创建空集合
a = set()
print(a)

# 字典转换为集合时,只会有健
a = set('hello')
a = set([1, 2, 3, 4, 5])
a = set({'a': 1, 'b': 2})
print(a)

# add 添加元素,无返回值
a = set()
a.add(1)
print(a)

# update 将一个集合添加到另一集合
a = {1, 2, 3}
b = {3, 4, 5}
a.update(b)
print(a)

# pop随机删除
a = {1, 2, 3}
a.pop()

# remove 指定删除
a = {'a', 'b', 'c'}
a.remove('a')
print(a)

# 练习
a = {1, 2, 3, 4, 5}
b = {3, 4, 5, 6, 7}

# 交集运算 &
print(a & b)

# 并集 |
print(a | b)

# 差集 -
print(a - b)

# 异或集 ^
print(a ^ b)

# 检查是不是子集 <=
a = {2, 3, 4}
b = {1, 2, 3, 4}
print(a <= b)

# 检查是不是真子集
a = {1, 2}
b = {1, 2, 3}
print(a < b)


# 检查是不是超集 >=
a = {1, 2, 3, 4}
b = {1, 2, 3, 4}
print(a >= b)

# 检查是不是真子集
a = {1, 2, 3}
b = {1, 2}
print(a > b)