简介
集合是由不重复元素组成的无序容器。集合的元素必须是可哈希的;即一个对象的哈希值在其生命周期内绝不改变,并且可以通其它对象进行比较;
集合作为一种无序的多项集,并不记录元素的位置或插入顺序,相应的集合不支持索引、切片等其他序列类的操作;
内置集合有两种类型:
- set:是可变的;可以使用add()和remove()这样的方法改变。它不可哈希,且不能作为字典的键或其他集合的元素
- frozenset:是不可变的且可哈希;其内容在被创建之后不能再改变,因此可作为字典的键或其他集合的元素;
集合常见的用途:
- 成员检测
- 序列去重
- 数学中的集合类计算;如:差集、并集、交集等;
集合操作
创建集合
集合创建的方式:
-
使用花括号内以逗号分隔元素的方式:
{'jack', 'sjoerd'}
-
使用集合推导式: {c for c in 'abracadabra' if c not in 'abc'}
-
使用类型构造器:
set()
,set('foobar')
,set(['a', 'b', 'foo'])
1. {}创建
格式:
s = {v1, v2, ...}
通过{}创建集合,注意空集合使用set函数创建哟
例子:
s = {'python', 'golang', 'html'}
# {'python', 'html', 'golang'}
2. set函数创建
格式:
s = set([iterable])
创建集合
- iterable:序列;如果为空,则创建空集合;
例子:
s = set('abcd')
# {'b', 'a', 'd', 'c'}
3. frozenset创建不可变集合
格式:
frozenset([iterable])
返回一个不可变的集合,一旦创建不可再添加和删除任何元素
- iterable:可迭代的对象,比如列表、字典、元组
例子:
s = frozenset(["python", "golang"])
# frozenset({'golang', 'python'})
4. 集合推导式创建
格式:
s = {x for x in iterable}
通过推导式创建结合;
例子:
s = {x for x in "abcd"}
# {'b', 'a', 'd', 'c'}
添加
add添加(仅用于可变集合)
格式:
s.add(elem)
将元素 elem 添加到集合s中,如果元素已经存在,则不进行任何操作;
例子:
s = set(('a', 'b', 'c'))
s.add('f')
# {'f', 'g', 'r', 't'}
更新
以下方法仅可用 set 而不能用于不可变的 frozenset 实例的操作:
1. update
格式:
s.update(*others)
用于修改当前集合;可以添加新的元素或集合到当前集合中,如果添加的元素在集合中已经存在,则忽略;
- *others:集合 或 任何可迭代对象
例子:
s1 = {'b', 'a', 'd', 'c'}
s2 = {'python', 'golang', 'html'}
s1.update(s2)
print(s1)
# {'a', 'html', 'golang', 'd', 'python', 'b', 'c'}
s1.update(['123', 'abc'])
# {'123', 'golang', 'b', 'abc', 'd', 'a', 'html', 'python', 'c'}
2. intersection_update
格式:
set.intersection_update(*others)
更新集合;只保留set集合中元素在所有others中也存在的集合;
*others:集合 或 任何可迭代对象
例子:
s = {'python', 'golang', 'html'}
s1 = {'html', 'css', 'js'}
s.intersection_update(s1)
# {'html'}
s = {'python', 'golang', 'html'}
s1 = {'html', 'css', 'js'}
s2 = {'redis', 'mysql', 'html'}
s.intersection_update(s1, s2)
# {'html'}
s = {'python', 'golang', 'html'}
s1 = {'html', 'css', 'js'}
s2 = {'redis', 'mysql', 'html'}
s3 = {'oracle', 'java'}
s.intersection_update(s1, s2, s3)
# set()
3. difference_update
格式:
set.difference_update(*others)
更新集合;移除set集合中存在于others中的元素;
- *others:集合 或 任何可迭代对象
例子:
s = {'python', 'golang', 'html'}
s1 = {'html', 'css', 'js'}
s.difference_update(s1)
# {'golang', 'python'}
s = {'python', 'golang', 'html'}
s1 = {'html', 'css', 'js'}
s2 = {'oracle', 'java', "golang"}
s.difference_update(s1, s2)
# {'python'}
s = {'python', 'golang', 'html'}
s1 = {'html', 'css', 'js'}
s2 = {'oracle', 'java'}
s.difference_update(s1, s2)
# {'golang', 'python'}
4. symmetric_difference_update
格式:
set.symmetric_difference_update(other)
更新集合;移除两个集合相同的元素;并将other中不同的元素插入到set集合中;
例子:
s = {'python', 'golang', 'html'}
s1 = {'html', 'css', 'js'}
s.symmetric_difference_update(s1)
# {'golang', 'python', 'js', 'css'}
s = {'python', 'golang', 'html'}
s.symmetric_difference_update(['python', 'java'])
# {'golang', 'html', 'java'}
移除
1. remove(仅用于可变集合)
格式:
s.remove(x)
从集合中删除指定的元素,如果删除的元素不存在则引发KeyError异常
例子:
s = {'123', 'golang', 'b', 'd', 'a', 'html','python', 'c'}
s.remove('golang')
# {'123', 'd', 'a', 'html', 'python', 'b', 'c'}
2. discard(仅用于可变集合)
格式:
s.discard(x)
从集合中删除指定的元素,当元素不存在时,不会触发异常;
例子:
s = {'123', 'golang', 'b', 'd', 'a', 'html','python', 'c'}
s.discard('123')
# {'d', 'a', 'html', 'golang', 'python', 'b', 'c'}
3. pop(仅用于可变集合)
格式:
s.pop()
随机删除集合中的一个元素,当集合为空时,会触发异常;
例子:
s = {'123', 'golang', 'b', 'd', 'a', 'html','python', 'c'}
s.pop()
# '123'
4. clear(仅用于可变集合)
格式:
s.clear()
移除集合中的所有元素;clear不会创建新集合,而是修改原有集合;
例子:
s = {'123', 'golang', 'b', 'd', 'a', 'html','python', 'c'}
s.clear()
# set()
5. del
格式:
del s
删除集合变量,删除变量之后,再次使用会触发异常
例子:
s = {'123', 'golang', 'b', 'd', 'a', 'html','python', 'c'}
del s
print(s)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 's' is not defined
检测是否在集合中
1. in
格式:
x in set
检测 x 是否在集合set中;
例子:
s = {'python', 'golang', 'html'}
print("python" in s)
# True
2. not in
格式:
x not in set
检测 x 是否不在集合set中
例子:
s = {'python', 'golang', 'html'}
print("java" not in s)
# True
3. isdisjoint
格式:
set.isdisjoint(other)
判断两个集合是否包含相同元素;如果没有返回True,反之返回False
- other:必须,要比较的集合
例子:
s = {'a', 'b', 'c', 'd'}
s1 = {'a', '1', 'v', 'f'}
s.isdisjoint(s1)
# False
s1 = {'1', 'v', 'f'}
s.isdisjoint(s1)
# True
4. issubset
格式:
set.issubset(other)
判断set集合的所有元素是否都包含在other集合中;即检测集合是否为other集合的真子集;如果是则返回True,反之返回False
- other:必须,要比较的集合
例子:
s = {'a', 'b', 'c', 'd'}
s1 = {'a', 'b'}
s1.issubset(s)
# True
5. issuperset
格式:
set.issuperset(other)
检测other集合的每个元素是否都在集合set中;即检测set集合是否为other集合的真超集;如果是则返回True,反之则返回False
例子:
s = {'a', 'b', 'c', 'd'}
s1 = {'a', 'b'}
s.issuperset(s1)
# True
集合并集、交集、差集
1. union
格式:
set.union(*others)
操作符:|
set1 | other1 | ...
返回一个新的集合;集合的并集,即包含了所有集合的元素,重复的元素只会出现一次;
- others:必须,合并的目标集合
例子:
s = {'a', 'b', 'c', 'd'}
s1 = {'a', 'f', 'g', 'l'}
x = s.union(s1)
# {'g', 'c', 'l', 'f', 'b', 'd', 'a'}
x = s | s1
# {'g', 'c', 'l', 'f', 'b', 'd', 'a'}
2. intersection
格式:
set.intersection(*others)
操作服:&
set * set1 * ... * setn
返回一个新的集合;集合间的交集;返回set集合与others所指定的所有集合的共有元素;
例子:
s = {'a', 'b', 'c', 'd'}
s1 = {'a', 'f', 'g', 'l'}
s2 = {'123', 'abc'}
s.intersection(s1)
# {'a'}
s.intersection(s2)
# set()
s.intersection(s1, s2)
# set()
s & s1
# {'a'}
s & s1 & s2
# set()
3. difference
格式:
set.difference(*others)
操作符:-
set - set1 ... setn
返回一个新集合;返回集合的差集;即返回set集合中不包含在others所指定的所有集合中的元素;
例子:
s = {'a', 'b', 'c', 'd'}
s1 = {'a', 'f', 'g', 'l'}
s2 = {'b', 'c', 'abc'}
s.difference(s1)
# {'b', 'c', 'd'}
s.difference(s2)
# {'a', 'd'}
s.difference(s1, s2)
# {'d'}
s - s1
# {'b', 'c', 'd'}
s - s2
# {'a', 'd'}
s - s1 - s2
# {'d'}
4. symmetric_difference
格式:
set.symmetric_difference(other)
返回一个新集合;返回两个集合中不同属于两者的元素;
- other:集合
例子:
s = {'a', 'b', 'c', 'd'}
s1 = {'a', 'f', 'g', 'l'}
s.symmetric_difference(s1)
# {'g', 'c', 'l', 'f', 'b', 'd'}