文章目录
python之Set操作(下)
5. Set删除
Set删除元素,以下操作对象是可变集合(del关键字删除变量除外)
1) 删除一个指定元素
Set删除一个指定元素
推荐写法: 使用Python3的标准库,set类内置函数:discard(elem),elem指定要删除的元素
- 修改当前集合,删除一个指定元素
- 若要删除的元素不存在,则不执行任何操作
set1 = {"hello", "world", 2, "python"}
set1.discard("python")
print(set1) # {'world', 'hello', 2}
set1.discard("java")
print(set1) # {'world', 'hello', 2}
一般写法: 使用Python3的标准库,set类内置函数:remove(elem),elem指定要删除的元素
- 修改当前集合,删除一个指定元素
- 若要删除的元素不存在,则抛出KeyError
set1 = {"hello", "world", 2, "python"}
set1.remove("python")
print(set1) # {'world', 'hello', 2}
set1.remove("java") # 抛出KeyError: 'java'
2) 随机删除一个元素
Set随机删除一个元素
推荐写法: 使用Python3的标准库,set类内置函数:pop()
- 修改当前集合,随机删除一个元素
- 若Set为空,则抛出KeyError
set1 = {"hello", "world", 2, "python"}
set1.pop()
print(set1) # {2, 'hello', 'python'}
empty_set = set()
empty_set.pop() # 抛出KeyError: 'pop from an empty set'
3) 清空Set
删除Set中所有元素
推荐写法: 使用Python3的标准库,set类内置函数:clear()
- 修改当前集合,删除Set中所有元素
set1 = {"hello", "world"}
set1.clear()
print(set1) # set()
4) 删除整个Set
删除整个Set变量
推荐写法: 使用关键字“del”
- del作用于变量而不是对象
- del只是解除变量和对象的引用关系,而不是销毁对象释放占用的内存空间
- python的垃圾回收机制主要采用的是引用计数的方式
set1 = {"hello", "world"}
del set1
print(set1) # 抛出NameError: name 'set1' is not defined
6. Set判断
1) 判断两个Set是否相等
判断两个Set是否相等
推荐写法: 使用运算符”==“
- 因为Set的无序性,所以对元素顺序不敏感
- 若两个集合长度和所包含元素相同,则返回True,否则返回False
a = {1, 2, 3}
b = {3, 1, 2}
print(a == b) # True
c = {"hello", "world", "python"}
d = {"python", "hello"}
print(c == d) # False
2) 判断两个Set有没有交集
如果Set a和Set b有相同元素,那么Set a和Set b有交集
推荐写法: 使用Python3的标准库,set类内置函数:isdisjoint(other),other指代另一个Set
- 若两个集合有交集(包括两个集合相等),则返回False,否则返回True
a = {"hello", "world"}
b = {"hello", "python"}
print(a.isdisjoint(b)) # False
print(a.isdisjoint(a)) # False
c = {1, 2, 3}
d = {4, 5, 6}
print(c.isdisjoint(d)) # True
3) 判断一个Set是否包含另一个Set
如果Set a中的元素涵盖Set b的全部元素,那么Set a包含Set b
推荐写法: 使用Python3的标准库,set类内置函数:issuperset(other),other指代另一个Set
- 若集合a包含集合b(包括两个集合相等和真包含关系),则返回True,否则返回False
- 等同于运算符”>=“
- 若要判断集合a是否真包含集合b,使用运算符”>“
a = {"hello", "world", "python"}
b = {"hello", "python"}
print(a.issuperset(b)) # True
print(a >= b) # True
print(a > b) # a真包含b True
print(a.issuperset(a)) # True
print(a >= a) # True
print(a > a) # False
c = {1, 2, 3}
d = {4, 5, 6}
print(c.issuperset(d)) # False
e = {1, 2, 3}
f = {2, 3, 4}
print(e.issuperset(f)) # False
4) 判断一个Set是否是另一个Set的子集
如果Set a的全部元素都在Set b中,那么Set a是Set b的子集
推荐写法: 使用Python3的标准库,set类内置函数:issuperset(other),other指代另一个Set
- 若集合a是集合b的子集(包括两个集合相等和真子集关系),则返回True,否则返回False
- 等同于运算符”<=“
- 若要判断集合a是否是集合b的真子集,使用运算符”<“
a = {"hello", "python"}
b = {"hello", "world", "python"}
print(a.issubset(b)) # True
print(a <= b) # True
print(a < b) # a是b的真子集 True
print(a.issubset(a)) # True
print(a <= a) # True
print(a < a) # False
c = {1, 2, 3}
d = {4, 5, 6}
print(c.issubset(d)) # False
e = {1, 2, 3}
f = {2, 3, 4}
print(e.issubset(f)) # False
5) 判断元素是否在Set中
判断元素是否在Set中
推荐写法: 使用运算符“in”
- 对大小写敏感
if elem in set1:
"""do something"""
if elem not in set1:
"""do something"""
set1 = {"hello", "world", "python"}
print("python" in set1) # True
print("python" not in set1) # False
print("Python" in set1) # False
7. Set转换
Set与String、Tuple、List相互转换
1) Set转String
推荐写法: 使用Python3的标准库,str类内置函数:str(obj),obj指代一个python对象
set1 = {"hello", "world"}
string = str(set1)
print(string) # "{'hello', 'world'}"
2) Set转Tuple
推荐写法: 使用Python3的标准库,tuple类内置函数:tuple([iterable]),iterable是可选参数,指代一个可迭代对象
set1 = {"hello", "world"}
tuple1 = tuple(set1)
print(tuple1) # ('hello', 'world')
3) Set转List
推荐写法: 使用Python3的标准库,list类内置函数:list([iterable]),iterable是可选参数,指代一个可迭代对象
set1 = {"hello", "world"}
list1 = list(set1)
print(list1) # ['hello', 'world']
8. Set运算
基于集合之间的包含关系,进行求并集、交集、差集、对称差集、补集的集合间运算
1) 并集
并集是指包含所有集合的元素的集合(重复元素只出现一次)。
*推荐写法: 使用Python3的标准库,set类内置函数:union(others),others指代其它Set
- 返回一个新Set,不会修改当前Set
- 接受多个Set对象参数,多个参数使用逗号”,“隔开
- 等同于运算符”|“
a = {"hello", "world"}
b = {"hello", "python"}
c = {1, 2, 3}
d = a.union(b, c)
print(d) # {1, 2, 'world', 'hello', 3, 'python'}
_d = a | b | c
print(_d) # {1, 2, 'world', 'hello', 3, 'python'}
print(a) # Set a的元素没有变化 {'hello', 'world'}
推荐写法: 使用Python3的标准库,set类内置函数:update(*others),others指代其它Set
- 修改当前集合,添加新元素到当前集合中
- 若添加的元素在集合中已存在,则该元素只会出现一次,重复的会被忽略
- 接受多个Set对象参数,多个参数使用逗号”,“隔开等同于运算符”|=“
a = {"hello", "world"}
_a = a.copy()
b = {"hello", "python"}
c = {1, 2, 3}
a.update(b, c)
print(a) # Set a的元素被修改 {'hello', 1, 2, 'python', 3, 'world'}
_a |= b
_a |= c
print(_a) # Set _a的元素被修改 {'hello', 1, 2, 'python', 3, 'world'}
2) 交集
交集是指包含所有集合的相同元素的集合(重复元素只出现一次)。
推荐写法: 使用Python3的标准库,set类内置函数:intersection(*others),others指代其它Set
- 返回一个新Set,不会修改当前Set
- 接受多个Set对象参数,多个参数使用逗号”,“隔开
- 等同于运算符”&“
a = {"hello", "world"}
b = {"hello", "world", "python"}
c = {"hello", "python"}
d = a.intersection(b, c)
print(d) # {'hello'}
_d = a & b & c
print(_d) # {'hello'}
print(a) # Set a的元素没有变化 {'hello', 'world'}
推荐写法: 使用Python3的标准库,set类内置函数:intersection_update(*others),others指代其它Set
- 修改当前集合,移除当前Set中与其它Set不同的元素
- 接受多个Set对象参数,多个参数使用逗号”,“隔开
- 等同于运算符”&=“
a = {"hello", "world"}
_a = a.copy()
b = {"hello", "world", "python"}
c = {"hello", "python"}
a.intersection_update(b, c)
print(a) # Set a的元素被修改 {'hello'}
_a &= b
_a &= c
print(_a) # Set _a的元素被修改 {'hello'}
3) 差集
差集是指包含当前集合特有元素的集合(重复元素只出现一次)。
推荐写法: 使用Python3的标准库,set类内置函数:difference(*others),others指代其它Set
- 返回一个新Set,不会修改当前Set
- 接受多个Set对象参数,多个参数使用逗号”,“隔开
- 等同于运算符”-“
a = {"hello", "world", 2, "python"}
b = {"hello", "world"}
c = {"hello", "python"}
d = a.difference(b, c)
print(d) # {2}
_d = a - b - c
print(_d) # {2}
print(a) # Set a的元素没有变化 {'hello', 'world', 2, 'python'}
推荐写法: 使用Python3的标准库,set类内置函数:difference_update(*others),others指代其它Set
- 修改当前集合,移除当前Set中与其它Set相同的元素
- 接受多个Set对象参数,多个参数使用逗号”,“隔开
- 等同于运算符”-=“
a = {"hello", "world", 2, "python"}
_a = a.copy()
b = {"hello", "world"}
c = {"hello", "python"}
a.difference_update(b, c)
print(a) # Set a的元素被修改 {2}
_a -= b
_a -= c
print(_a) # Set _a的元素被修改 {2}
4) 补集
- 补集包括绝对补集(两个集合是包含关系)和相对补集(两个集合有交集,但并不是包含关系);
- A相对于B的补集是指B中移除与A相同的元素的集合(重复元素只出现一次)。
推荐写法: 使用运算符“-” - 返回一个新Set,不会修改当前Set
a = {"hello", "world", 2, "python"}
b = {"hello", "world"}
c = {"hello", "python"}
# b相对于a的绝对补集(a包含b)
b_a = a - b
print(b_a) # {2, 'python'}
# b相对于c的相对补集(c和b有交集但是c不包含b)
b_c = c - b
print(b_c) # {'python'}
来源:CSDN
作者:我行于野
链接:https://blog.csdn.net/weixin_45080696/article/details/102728397