简介
集合(set)是Python中标准数据类型之一,这个数据类型跟我们数学中的集合概念一样。它也是容器类型,存储着无序不重复的数据。集合元素可以做字典中的键,因此,我们应该想到集合中的元素必须为不可变类型(数字、字符串、元组)。
集合的定义
将数据用{}包含的数据类型称为集合,这与字典很像,但是字典包含的是键值对。集合有两种创建方式:
直接创建:
>>>a = {'a', 1, (2, 3)} #无序的
>>>a
{(2, 3), 1, 'a'}
1
2
3
>>>a={'a',1,(2,3)}#无序的
>>>a
{(2,3),1,'a'}
调用set()函数:
>>>a = set('finthon.com') #自动去重
>>>a
{'.', 'c', 'f', 'h', 'i', 'm', 'n', 'o', 't'}
1
2
3
>>>a=set('finthon.com')#自动去重
>>>a
{'.','c','f','h','i','m','n','o','t'}
集合的特点
所谓存在即合理,集合虽然使用的场合很少,但是也有自己适应的场合。集合的特点如下:
集合中的元素是无序、不重复的值;
集合中的元素是不可变数据类型;
集合中的元素不能通过切片工具访问;
可以进行集合的运算。
访问集合中的值
虽然不能通过切片工具访问集合中的值,但是我们可以判断一个值是否存在集合中,或者遍历这个集合:
>>>a = set('finthon.com')
>>>a
{'.', 'c', 'f', 'h', 'i', 'm', 'n', 'o', 't'}
>>>'c' in a
True
>>>for i in a: #for循环,打印集合中的值
... print(i)
...
t
h
c
i
m
n
.
f
o
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
>>>a=set('finthon.com')
>>>a
{'.','c','f','h','i','m','n','o','t'}
>>>'c'ina
True
>>>foriina:#for循环,打印集合中的值
...print(i)
...
t
h
c
i
m
n
.
f
o
集合中添加值
有两种向集合添加值的方法:
使用add()方法:
>>>a = set()
>>>a.add('a')
>>>a
{'a'}
1
2
3
4
>>>a=set()
>>>a.add('a')
>>>a
{'a'}
使用update()方法:
>>>a.update('com')
>>>a
{'a', 'c', 'm', 'o'}
1
2
3
>>>a.update('com')
>>>a
{'a','c','m','o'}
删除集合中的值
有五种删除集合中的值的方法可供选择:
使用remove()方法:
>>>a = set('finthon.com')
>>>a.remove('f')
>>>a
{'.', 'c', 'h', 'i', 'm', 'n', 'o', 't'}
1
2
3
4
>>>a=set('finthon.com')
>>>a.remove('f')
>>>a
{'.','c','h','i','m','n','o','t'}
使用discard()方法:
>>>a = set('finthon.com')
>>>a.discard('.')
>>>a
{'c', 'f', 'h', 'i', 'm', 'n', 'o', 't'}
1
2
3
4
>>>a=set('finthon.com')
>>>a.discard('.')
>>>a
{'c','f','h','i','m','n','o','t'}
使用pop()方法:
>>>a = set('finthon.com')
>>>a.pop() #删除任意一个值并返回,在交互模式下返回最后一个值
't'
>>>a
{'.', 'c', 'f', 'h', 'i', 'm', 'n', 'o'}
1
2
3
4
5
>>>a=set('finthon.com')
>>>a.pop()#删除任意一个值并返回,在交互模式下返回最后一个值
't'
>>>a
{'.','c','f','h','i','m','n','o'}
使用clear()方法:
>>>a = set('finthon.com')
>>>a.clear()
>>>a
set()
1
2
3
4
>>>a=set('finthon.com')
>>>a.clear()
>>>a
set()
使用del语句:
>>>del a
1
>>>dela
集合方法
现将集合方法总结如下:
方法
描述
set.add()
为集合添加元素
set.clear()
移除集合中的所有元素
set.copy()
拷贝一个集合
set1.difference(set2)
返回一个新集合,该集合是set1的元素,但不是set2的元素
set1.difference_update(set2)
set1中属于set1但不包含在set2中的元素
set.discard()
删除集合中指定的元素
set1.intersection(set2)
返回集合set1和set2的交集
set1.intersection_update(set2)
set1中属于set1和set2的元素
set1.isdisjoint(set2)
判断两个集合是否包含相同的元素,如果没有返回 True,否则返回 False
set1.issubset(set2)
判断指定集合set1是否为集合set2的子集
set1.issuperset(set2)
判断集合set2是否为集合set1的子集
set.pop()
随机移除元素
set.remove()
移除指定元素
set1.symmetric_difference(set2)
返回一个新集合,该集合是set1和set2的元素,但不是两者共有的元素
set1.symmetric_difference_update(set2)
set1中元素是set1和set2的元素,但不是两者共有的元素
set1.union(set2)
返回两个集合的并集
set.update()
给集合添加元素
代码演示:
>>>a = {1, 2, 3, 4}
>>>b = a.copy()
>>>a
{1, 2, 3, 4}
>>>b
{1, 2, 3, 4}
>>>c = {3, 4, 5, 6} #返回差集,原来的集合a,c不变
>>>x = a.difference(c)
>>>x
{1, 2}
>>>a
{1, 2, 3, 4}
>>>c
{3, 4, 5, 6}
>>>a.difference_update(c) #直接改变原来的集合a
>>>a
{1, 2}
>>>a = {1, 2, 3} #返回交集,原来的集合a,c不变
>>>b = {2, 3, 4}
>>>c = a.intersection(b)
>>>c
{2, 3}
>>>a.intersection_update(b) #直接改变原来的集合a
>>>a
{2, 3}
>>>a = {2, 3} #判断是否包含相同的元素,如果没有返回True
>>>b = {4, 5}
>>>a.isdisjoint(b)
True
>>>a = {1, 4} #a是否为b的子集
>>>b = {1, 2, 3, 4}
>>>a.issubset(b)
True
>>>a.issuperset(b) #b是否为a的子集
False
>>>a = {2, 3, 4} #返回两个集合不重复的元素,原集合a,b不变
>>>b = {4, 5, 6}
>>>c = a.symmetric_difference(b)
>>>c
{2, 3, 5, 6}
>>>a.symmetric_difference_update(b) #直接改变原集合a
>>>a
{2, 3, 5, 6}
>>>a = {1, 2}
>>>b = {3, 4}
>>>c = a.union(b)
>>>c
{1, 2, 3, 4}
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
>>>a={1,2,3,4}
>>>b=a.copy()
>>>a
{1,2,3,4}
>>>b
{1,2,3,4}
>>>c={3,4,5,6}#返回差集,原来的集合a,c不变
>>>x=a.difference(c)
>>>x
{1,2}
>>>a
{1,2,3,4}
>>>c
{3,4,5,6}
>>>a.difference_update(c)#直接改变原来的集合a
>>>a
{1,2}
>>>a={1,2,3}#返回交集,原来的集合a,c不变
>>>b={2,3,4}
>>>c=a.intersection(b)
>>>c
{2,3}
>>>a.intersection_update(b)#直接改变原来的集合a
>>>a
{2,3}
>>>a={2,3}#判断是否包含相同的元素,如果没有返回True
>>>b={4,5}
>>>a.isdisjoint(b)
True
>>>a={1,4}#a是否为b的子集
>>>b={1,2,3,4}
>>>a.issubset(b)
True
>>>a.issuperset(b)#b是否为a的子集
False
>>>a={2,3,4}#返回两个集合不重复的元素,原集合a,b不变
>>>b={4,5,6}
>>>c=a.symmetric_difference(b)
>>>c
{2,3,5,6}
>>>a.symmetric_difference_update(b)#直接改变原集合a
>>>a
{2,3,5,6}
>>>a={1,2}
>>>b={3,4}
>>>c=a.union(b)
>>>c
{1,2,3,4}
总结
我们介绍了集合(set)数据类型,事实上这种数据类型使用的场合比较少。它的好处是能够去掉重复数据,使用数学上的集合运算。至此,Python中六种标准数据类型就介绍完毕。