集合初始化和集合对象方法
前言
1111
集合(Set)
- Set 可以改变,它不是序列 无序性
- (集合元素是没有顺序的)
- 不重复性(元素是不重复的,即使有多个相同元素也会去重)
- 集合里只能包含不可变的数据类型
- 可以使用花括号 { } 或者 set() 函数创建集合
- 创建空集合必须用 set(),因为 { } 是用来创建空字典的
set01 = {1, 2, "4", (5, 6)}
print(set01) # {(5, 6), 1, 2, '4'}
set02 = set() # 定义空集合
print(set02) # set()
dict01 = {} # 定义空字典
print(type(dict01)) # <class 'dict'>
set([iterable])
- 返回一个新的 set 对象,其元素来自于 iterable,如果未指定 iterable,则将返回空集合
print(set()) # set() 空集合
print(set("China")) # {'a', 'C', 'i', 'h', 'n'}
print(set([1, 2, 3])) # {1, 2, 3}
print(set((1, 2, 3))) # {1, 2, 3}
print(set({1: 2, 3: 4})) # {1, 3}
frozenset([iterable])
- 返回一个新的 frozenset 对象,即不可变的集合,其元素来自于 iterable,如果未指定参数,则返回冻结的空集合
- 作用:set 中的元素必须是不可变类型的,而 frozenset 是可以作为 set 元素的
print(frozenset()) # frozenset()
print(frozenset("1234")) # frozenset({'4', '3', '1','2'})
print(frozenset([1, 2, 3, 4])) # frozenset({1, 2, 3, 4})
print(frozenset({"one": 1, "two": 2, "three": 3})) #
frozenset({'two', 'three', 'one'})
set1 = {"four"}
set1.add(frozenset({'two', 'three', 'one'}))
print(set1) # {frozenset({'one', 'two', 'three'}),'four'}
利用集合特性,可以用来 去重 和 关系测试
# 把一个列表变成集合,就会自动去掉重复的元素
li = [1, 2, 4, 5, 7, 7, 4, 5]
a = set(li)
print(a) # {1, 2, 4, 5, 7}
# 测试多组集合数据之间的交集、差集、并集等关系
a = set("abdefga")
b = set("abc")
c = set("aef")
print(a) # {'f', 'a', 'g', 'd', 'b', 'e'} {'b', 'a', 'c'} {'f', 'a', 'e'}
print(b) # {'b', 'a', 'c'}
print(c) # {'f', 'a', 'e'}
print(c <= a) # 判断c是否是a的子集 True
print(a - b) # 返回a和b的差集 {'e', 'f', 'g', 'd'}
print(a | b) # 返回a和b的并集 {'d', 'e', 'f', 'g', 'a', 'b', 'c'}
print(a & b) # 返回a和b的交集 {'a', 'b'}
print(a ^ b) # 返回a和b中不同时存在的元素(对称差) {'d', 'e', 'f', 'g', 'c'}
print(a | b | c) # {'d', 'e', 'f', 'g', 'a', 'b', 'c'}
集合的对象方法(set 和 frozenset 对象都可用)
isdisjoint(other)
- other:Iterable
- 如果集合中没有与 other 共有的元素则返回 True
- 此处如果字典作为参数传入,我们与字典的值作比对,不与键作比对。
str1 = "145"
list1 = [1, 4]
dic1 = {1: "1"}
set1 = {"1", 2, 3}
print(set1.isdisjoint(str1)) # False
print(set1.isdisjoint(list1)) # True
print(set1.isdisjoint(dic1)) # True
fset = frozenset(["1", 2, 3])
print(fset.isdisjoint(str1)) # False
print(fset.isdisjoint(list1)) # True
print(fset.isdisjoint(dic1)) # True
issubset(other)
- other:Iterable
- 如果集合中的每个元素都在 other 之中,则返回 True
- 对应的运算符版本 set <= other 要求参数为集合
- 此处如果字典作为参数传入,我们与字典的键作比对,不与字典的值作比对。
str1 = "132"
list1 = [1, 4, "1", "2"]
dic1 = {1: "1", 2: "2"}
set1 = {"1", "2"}
print(set1.issubset(str1)) # True
print(set1.issubset(list1)) # True
print(set1.issubset(dic1)) # False
fset = frozenset(["1", "2"])
print(fset.issubset(str1)) # True
print(fset.issubset(list1)) # True
print(fset.issubset(dic1)) # False
issuperset(other)
- other:Iterable
- 如果 other 中的每个元素都在集合之中,则返回 True
- 对应的运算符版本 set >= other 要求参数为集合
str1 = "12"
list1 = [1, "2"]
dic1 = {1: "1", 2: "2"}
set1 = {"1", "2", 1, 3}
print(set1.issuperset(str1)) # True
print(set1.issuperset(list1)) # True
print(set1.issuperset(dic1)) # False
fset = frozenset(["1", "2", 1, 3])
print(fset.issuperset(str1)) # True
print(fset.issuperset(list1)) # True
print(fset.issuperset(dic1)) # False
union(*others)
- others:Iterable
- 返回一个新集合,其中包含来自原集合以及 others 指定的所有集合中 的元素(即并集)
- 对应的运算符版本 set | other | … 要求参数为集合
str1 = "12"
list1 = [1, "2"]
dic1 = {1: "1", 2: "2"}
set1 = {"1", "2", 1, 3}
print(set1.union(str1, list1, dic1)) # {1, 2, 3, '2', '1'}
fset = frozenset(["1", "2", 1, 3])
print(fset.union(str1, list1, dic1)) # frozenset({1, 2, 3, '2', '1'})
intersection(*others)
- others:Iterable
- 返回一个新集合,其中包含原集合以及 others 指定的所有集合中共有 的元素(即交集)
- 对应的运算符版本 set & other & … 要求参数为集合
str1 = "1234"
list1 = [1, "2"]
dic1 = {"1": "3", "2": "4"} # 字典只有‘键’会参与到这里的交集判定
set1 = {"1", "2", 1, 3, "3", "4"}
print(set1.intersection(str1, list1, dic1)) # {'2'}
fset = frozenset(["1", "2", 1, 3])
print(fset.intersection(str1, list1, dic1)) # frozenset({'2'})
difference(*others
- others:Iterable
- 返回一个新集合,其中包含原集合中在 others 指定的其他集合中不存 在的元素(即差集)
- 对应的运算符版本 set - other - … 要求参数为集合
str1 = "12"
list1 = [1, "2"]
dic1 = {"1": 1, "2": 2}
set1 = {"1", "2", 1, 3}
print(set1.difference(str1, list1, dic1)) # {3}
fset = frozenset(["1", "2", 1, 3])
print(fset.difference(str1, list1, dic1)) # frozenset({3})
symmetric_difference(other)
- other:Iterable
- 返回一个新集合,其中的元素或属于原集合或属于 other 指定的其他 集合,但不能同时属于两者(即对称差)
- 对应的运算符版本 set ^ other 要求参数为集合
str1 = "12"
list1 = [1, "2"]
dic1 = {1: "1", 2: "2"}
set1 = {"1", "2", 1, 3}
print(set1.symmetric_difference(str1)) # {1, 3}
print(set1.symmetric_difference(list1)) # {"1", 3}
print(set1.symmetric_difference(dic1)) # {"1", "2", 3, 2}
fset = frozenset(["1", "2", 1, 3])
print(fset.symmetric_difference(str1)) # frozenset({3, 1})
print(fset.symmetric_difference(list1)) # frozenset({3, '1'})
print(fset.symmetric_difference(dic1)) # frozenset({2, 3, '1', '2'})
copy()
- 返回原集合的浅拷贝
set1 = {"1", "2", 1, 3}
set2 = set1.copy()
print(set2)
fset1 = frozenset(["1", "2", 1, 3])
fset2 = fset1.copy()
print(fset2)
集合的对象方法( 都仅set对象可用)
set.update(*others)
- others:Iterable
- 更新集合,添加来自 others 中的所有元素
str1 = "12"
list1 = [1, "2"]
dic1 = {"1": 1, "2": 2}
set1 = {1, 3}
set1.update(str1, list1, dic1)
print(set1) # {1, 3, "1", "2"}
set.intersection_update(*others)
- others:Iterable
- 更新集合,只保留其中在所有 others 中也存在的元素
str1 = "12"
list1 = [1, "2"]
dic1 = {"1": 1, "2": 2}
set1 = {1, 3, "1", "2"}
set1.intersection_update(str1, list1, dic1)
print(set1) # {"2"}
set.difference_update(*others)
- others:Iterable
- 更新集合,移除其中也存在于任意一个 others 中的元素
str1 = "12"
list1 = [1, "2"]
dic1 = {"1": 1, "2": 2}
set1 = {1, 3, "1", "2"}
set1.difference_update(str1, list1, dic1)
print(set1) # {3}
set.symmetric_difference_update(other)
- other:Iterable
- 更新集合,只保留存在于一方而非共同存在的元素
str1 = "124"
set1 = {1, 3, "1", "2"}
set1.symmetric_difference_update(str1)
print(set1) # {1, '4', 3}
set.add(elem)
- 将元素 elem 添加到集合中。如果元素已经存在,则没有影响
a = {1, 2, 3}
a.add("hello world")
print(a) # {1, 2, 3, 'hello world'}
set.remove(elem)
- 从集合中移除元素 elem。 如果 elem 不存在于集合中则会引发 KeyError
a = {1, 2, 3, 4}
a.remove(3)
print(a) # {1, 2, 4}
# a.remove(3)
set.discard(elem)
- 从集合中移除元素 elem。 如果 elem 不存在于集合中则不做任何操作
a = {1, 2, 3, 4}
a.discard(3)
print(a)
a.discard(3) # {1, 2, 4}
set.pop()
- 从集合中移除并返回任意一个元素。如果集合为空则会引发 KeyError
a = {1, 2}
elem1 = a.pop()
print(elem1) # 1
print(a) # {2}
elem2 = a.pop()
print(elem2) # 2
print(a) # set()
# a.pop()
set.clear()
- 从集合中移除所有元素
a = {1, 2, 3, 4}
a.clear()
print(a) # set()