Python - Set
2023-09-17 22:16:16
是不同可哈希对象的可变集合,与 list 和 tuple 相同。 它是对象的无序集合,这意味着它不记录元素位置或插入顺序,因此无法使用索引访问元素。
该集合是数学中集合的 Python 实现。集合对象具有合适的方法来执行数学集合运算,如并集、交集、差分等。
set 对象包含一个或多个项,不一定是同一类型,这些项用逗号分隔并括在大括号 {} 中。 下面定义一个带有偶数的 set 对象。
示例: Python Set Object
even_nums = {2, 4, 6, 8, 10} # set of even numbers
emp = {1, 'Steve', 10.5, True} # set of different objects
集合不存储重复的对象。即使在大括号内多次添加对象,在设置对象中也只保留一个副本。 因此,无法对设置对象执行索引和切片操作。
示例: Set of Distinct Elements
nums = {1, 2, 2, 3, 4, 4, 5, 5}
print(nums) #output: {1, 2, 3, 4, 5}
集合中元素的顺序不一定与赋值时给出的顺序相同。 Python 优化了集合的结构,以便对其执行操作,如数学中所定义的那样。
只有不可变(和可哈希)对象才能成为集合对象的一部分。接受数字(整数、浮点数和复数)、字符串和元组对象,但不接受 set、list 和字典对象。
示例: Set Elements
myset = {(10,10), 10, 20}
print(myset)
myset = {[10, 10], 10, 20} #TypeError can't add a list
myset = { {10, 10}, 10, 20} #TypeError can't add a set
在上面的例子中,(10,10)
是一个元组,因此它成为集合的一部分。但是,[10,10]
是一个列表,因此会显示一条错误消息,指出该列表不可散列。 (Hashing是计算机科学中的一种机制,可以更快地搜索计算机内存中的对象。
即使可变对象不存储在集中,集合本身也是一个可变对象。
使用该set() function创建一个空集。空大括号将创建一个空dictionary而不是空集。
示例: Creating an Empty Set
emp = {} # creates an empty dictionary
print(type(emp)) #<class 'dict'>
s = set() # creates an empty set
print(type(s)) #<class 'set'>
set() function还用于将字符串、元组或字典对象转换为 set 对象,如下所示。
示例: Convert Sequence to Set
s = set('Hello') # converts string to set
print(s) #output: {'l', 'H', 'o', 'e'}
s = set((1,2,3,4,5)) # converts tuple to set
print(s) #output: {1, 2, 3, 4, 5}
d = {1:'One', 2: 'Two'}
s = set(d) # converts dict to set
print(s) #{1, 2}
修改集合元素
使用内置的集合函数add()、remove()或update()方法来修改集合集合。
示例:
s = set() # creates an empty set
s.add(10) # add an element
s.add(20)
s.add(30)
print(s) #output: {10, 20, 30}
primeNums = {2, 3, 5, 7}
s.update(primeNums) # update set with another set
print(s) #output:{2, 3, 20, 5, 7, 10, 30}
s.remove(2) # remove an element
print(s) #output:{3, 20, 5, 7, 10, 30}
set operations
如前所述,Python 中的集合数据类型实现为数学中定义的集合。 可以执行各种设置操作。运算符 |、&、- 和 ^ 分别执行并集、交集、差分和对称差分运算。这些运算符中的每一个都有一个与内置 set 类关联的相应方法。
Operation | 例子 |
---|---|
Union: Returns a new set with elements from both the sets. Operator: | Method: set.union() |
s1={1,2,3,4,5} s2={4,5,6,7,8} s1|s2 #{1, 2, 3, 4, 5, 6, 7, 8}Try it |
s1={1,2,3,4,5} s2={4,5,6,7,8} s1.union(s2) #{1, 2, 3, 4, 5, 6, 7, 8} s2.union(s1) #{1, 2, 3, 4, 5, 6, 7, 8}Try it |
|
Intersection: Returns a new set containing elements common to both sets. Operator: & Method: set.intersection() |
s1={1,2,3,4,5} s2={4,5,6,7,8} s1&s2 #{4, 5} s2&s1 #{4, 5}Try it |
s1={1,2,3,4,5} s2={4,5,6,7,8} s1.intersection(s2) #{4, 5} s2.intersection(s1) #{4, 5}Try it |
|
Difference: Returns a set containing elements only in the first set, but not in the second set. 运算符: - Method: set.difference() |
s1={1,2,3,4,5} s2={4,5,6,7,8} s1-s2 #{1, 2, 3} s2-s1 #{8, 6, 7}Try it |
s1={1,2,3,4,5} s2={4,5,6,7,8} s1.difference(s2) #{1, 2, 3} s2.difference(s1) #{8, 6, 7}Try it |
|
Symmetric Difference: Returns a set consisting of elements in both sets, excluding the common elements. Operator: ^ Method: set.symmetric_difference() |
s1={1,2,3,4,5} s2={4,5,6,7,8} s1^s2 #{1, 2, 3, 6, 7, 8} s2^s1 #{1, 2, 3, 6, 7, 8}Try it |
s1={1,2,3,4,5} s2={4,5,6,7,8} s1.symmetric_difference(s2) #{1, 2, 3, 6, 7, 8} s2.symmetric_difference(s1) #{1, 2, 3, 6, 7, 8}Try it |
设置方法
下表列出了内置的 set 方法:
函数 | 描述 |
---|---|
set.add() | 将元素添加到集合中。如果集合中已存在某个元素,则不会添加该元素。 |
set.clear() | 从集合中删除所有元素。 |
set.copy() | 返回集的浅表副本。 |
set.difference() | 返回新集,其中包含不作为参数传递的另一个集中的唯一元素。 |
set.difference_update() | 使用作为参数传递的另一个集中的通用元素更新调用该方法的集合。 |
set.discard() | 从集合中删除特定元素。 |
set.intersection() | 返回包含给定集中通用元素的新集。 |
set.intersection_update() | 更新调用 instersection_update() 方法的集合,使用指定集合中的公共元素。 |
set.isdisjoint() | 如果给定集合没有公共元素,则返回 true。集合是不相交的,当且仅当它们的交集是空集合。 |
set.issubset() | 如果集合(调用 issubset() 的集合)包含作为参数传递的另一个集合的每个元素,则返回 true。 |
set.pop() | 从集合中删除并返回随机元素。 |
set.remove() | 从集合中删除指定的元素。如果未找到指定的元素,则引发错误。 |
set.symmetric_difference() | 返回一个新集,其中包含在两个集中找到的不同元素。 |
set.symmetric_difference_update() | 使用指定集合中通用的元素更新调用 instersection_update() 方法的集合。 |
set.union() | 返回一个新集,其中包含来自所有给定集的不同元素。 |
set.update() | 通过从传递的一个或多个可迭代对象中添加不同的元素来更新集合。 |