1 集合的定义和特点
- (1) 集合是用花括号括起来的,集合的特点是元素没有顺序,元素具有唯一性,不能重复
>>> a={1,2,3,4}
>>> type(a)
<class 'set'>
>>> a={1,2,3,1,2,3}
>>> a
{1, 2, 3}
2 集合的常用运算
- (1)集合元素没有顺序,所以不能像列表和元组那样用下标取值
>>> a={1,2,3}
>>> a[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'set' object is not subscriptable
>>> [1,2,3]*3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
>>> {1,2,3}+{4,5,6}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'set' and 'set'
- (3)len() 函数返回集合的长度,即集合中元素的个数
>>> a={1,2,3,4,5}
>>> len(a)
5
>>> a={1,2,3,4,5}
>>> max(a)
5
>>> a={1,2,3,4,5}
>>> min(a)
1
- (6)"-"表示两个集合差集,A-B,即在A中不在B中的元素
>>> a={1,2,3,4,5,6}
>>> b={4,5,6,7,8,9}
>>> a-b
{1, 2, 3}
- (7) "|"表示两个集合的并集,A | B, 表示A,B中所有元素的集合
>>> a={1,2,3}
>>> b={4,5,6}
>>> a | b
{1, 2, 3, 4, 5, 6}
- (8) "&"表示两个集合的交集,A & B,表示既在A中又在B中的集合
>>> a={1,2,3,4,5,6}
>>> b={4,5,6,7,8,9}
>>> a & b
{4, 5, 6}
- (9) in,not in 判断集合中是否有某一元素
>>> a={1,2,3,4,5,6}
>>> 0 in a
False
>>> 0 not in a
True
>>> 3 in a
True
>>> a={1,2,3}
>>> sum(a)
6
3 集合常用的函数
- (1) add(elem) 向集合中增加一个元素,如果此元素已经存在于集合中,则不作任何处理
>>> a={1,2,3}
>>> a.add(4)
>>> a
{1, 2, 3, 4}
>>> a.add(1)
>>> a
{1, 2, 3, 4}
>>> a={1,2,3}
>>> a
{1, 2, 3}
>>> a.clear()
>>> a
set()
>>> a={1,2,3,4}
>>> b=a.copy()
>>> b
{1, 2, 3, 4}
>>> a={1,2,3,4,5}
>>> a
{1, 2, 3, 4, 5}
>>> b=a.pop()
>>> b
1
>>> a
{2, 3, 4, 5}
- (5)remove(elem) 从集合中去除某元素,若集合中没有此元素则会报错
>>> a={1,2,3,4,5}
>>> a.remove(3)
>>> a
{1, 2, 4, 5}
>>> a.remove(7)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 7
- (6)union(set) 计算两个集合的合集,并返回一个新的集合,原来的两个集合没有变化
>>> a={1,2,3,4,5,6}
>>> b={3,4,5,6,7,8}
>>> a
{1, 2, 3, 4, 5, 6}
>>> b
{3, 4, 5, 6, 7, 8}
>>> c=a.union(b)
>>> c
{1, 2, 3, 4, 5, 6, 7, 8}
>>> a
{1, 2, 3, 4, 5, 6}
>>> b
{3, 4, 5, 6, 7, 8}
- (7)update(set) 将set集合与原集合计算并集,并更新至原集合
>>> a={1,2,3,4,5,6}
>>> b={3,4,5,6,7,8}
>>> a
{1, 2, 3, 4, 5, 6}
>>> b
{3, 4, 5, 6, 7, 8}
>>> a.update(b)
>>> a
{1, 2, 3, 4, 5, 6, 7, 8}
>>> b
{3, 4, 5, 6, 7, 8}
- (8)difference(set) 计算集合的差集,和“-”运算符一致
>>> a={1,2,3,4,5,6}
>>> b={4,5,6,7,8,9}
>>> a.difference(b)
{1, 2, 3}
>>> a
{1, 2, 3, 4, 5, 6}
>>> b
{4, 5, 6, 7, 8, 9}
- (9)difference_update(set) 计算差集,将结果更新至原集合
>>> a={1,2,3,4,5,6}
>>> b={4,5,6,7,8,9}
>>> a
{1, 2, 3, 4, 5, 6}
>>> b
{4, 5, 6, 7, 8, 9}
>>> a.difference_update(b)
>>> a
{1, 2, 3}
>>> b
{4, 5, 6, 7, 8, 9}
- (10)discard(elem) 与remove(elem)功能一致,只不过discard移除的元素若不存在,不会报错
>>> a={1,2,3,4,5,6}
>>> a.discard(4)
>>> a
{1, 2, 3, 5, 6}
>>> a.discard(10)
>>> a
{1, 2, 3, 5, 6}
- (11)intersection(set) 计算两个集合的交集,结果生成新的集合,原有的集合不变
>>> a={1,2,3,4,5,6}
>>> b={4,5,6,7,8,9}
>>> a
{1, 2, 3, 4, 5, 6}
>>> b
{4, 5, 6, 7, 8, 9}
>>> a.intersection(b)
{4, 5, 6}
>>> a
{1, 2, 3, 4, 5, 6}
>>> b
{4, 5, 6, 7, 8, 9}
- (12)intersection_update(set) 计算两个交集,结果更新至原有集合
>>> a={1,2,3,4,5,6}
>>> b={4,5,6,7,8,9}
>>> a
{1, 2, 3, 4, 5, 6}
>>> b
{4, 5, 6, 7, 8, 9}
>>> a.intersection_update(b)
>>> a
{4, 5, 6}
>>> b
{4, 5, 6, 7, 8, 9}
- (13)isdisjoint(set) 判断两个是否有公共元素,若没有返回True,否则返回False
>>> a={1,2,3,4,5,6}
>>> b={4,5,6,7,8,9}
>>> c={7,8,9,0}
>>> a.isdisjoint(b)
False
>>> a.isdisjoint(c)
True
- (14)issubset(set) 判断是否为子集,若是set的子集,返回True,否则返回False
>>> a={1,2,3,4}
>>> b={1,2,3,4,5}
>>> a.issubset(b)
True
>>> c={2,3,4,5}
>>> a.issubset(c)
False
- (15)issuperset(set) 判断是否为超集,若是返回True,否则返回False
>>> a={1,2,3,4,5,6}
>>> b={1,2,3,4}
>>> c={1,2,3,4,5,6,7,8}
>>> a.issuperset(b)
True
>>> a.issuperset(c)
False
- (17)symmetric_difference(set) 返回两个集合的不重复的元素,原有的集合不变
>>> a={1,2,3,4,5,6}
>>> b={4,5,6,7,8,9}
>>> a
{1, 2, 3, 4, 5, 6}
>>> b
{4, 5, 6, 7, 8, 9}
>>> a.symmetric_difference(b)
{1, 2, 3, 7, 8, 9}
>>> a
{1, 2, 3, 4, 5, 6}
>>> b
{4, 5, 6, 7, 8, 9}
- (17)symmetric_difference_update(set) 返回两个集合的不重复的元素,并将结果更新至原有集合
>>> a={1,2,3,4,5,6}
>>> b={4,5,6,7,8,9}
>>> a
{1, 2, 3, 4, 5, 6}
>>> b
{4, 5, 6, 7, 8, 9}
>>> a.symmetric_difference_update(b)
>>> a
{1, 2, 3, 7, 8, 9}
>>> b
{4, 5, 6, 7, 8, 9}