Python 集合

Python 集合可以存储多个项目,集合中的项目是无序的,不可重复的,集合是可变的。

Python 集合可以存储多个项目,集合中的项目是无序的,不可重复的,集合是不可变的。

集合是 Python 中用于存储数据集合的 4 种内置数据类型之一,其他 3 种是列表元组字典

创建 Python 集合可以使用集合字面量(大括号 {})或者 set() 构造函数。

thisset = {"apple", "banana", "cherry"}
thisset = set(("apple", "banana", "cherry"))

Python 集合具有如下特征:

  • 集合中的项目不保证顺序的。
  • 集合是可变的。
  • 集合中项目不可以重复。即使你添加了重复项目,也只保留一份。
  • 集合中的项目的数据类型可以不同。

集合的类型

Python 集合的数据类型为 <class 'set'>

打印集合的数据类型:

myset = {"apple", "banana", "set"}
print(type(myset))

访问项目

集合不能通过索引或键来访问中其中的项目,但是可以使用 for 循环遍历集合项,或者使用 in 关键字检查集合中是否存在指定的值 。

循环遍历集合,并打印值:

thisset = {"apple", "banana", "cherry"}

for x in thisset:
  print(x)

输出:

cherry
apple
banana

检查集合中是否存在“香蕉”:

thisset = {"apple", "banana", "cherry"}
print("banana" in thisset) # True

更改项目

创建集合后,您无法更改其项目,但可以添加新项目。

添加项目

使用 add() 方法将项目添加到集合中。

thisset = {"apple", "banana", "cherry"}
thisset.add("orange")
print(thisset)  # {'cherry', 'apple', 'orange', 'banana'}

添加集

update() 方法可将另一个集合中的项目添加到当前集合中。

thisset = {"apple", "banana", "cherry"}
tropical = {"pineapple", "mango", "papaya"}

thisset.update(tropical)

print(thisset) # {'cherry', 'banana', 'papaya', 'apple', 'pineapple', 'mango'}

添加任何可迭代对象

update() 方法中的参数可以是任何可迭代对象(元组、列表、字典等)。

将列表的项目添加到集合中:

thisset = {"apple", "banana", "cherry"}
mylist = ["kiwi", "orange"]

thisset.update(mylist)

print(thisset) # {'cherry', 'kiwi', 'banana', 'orange', 'apple'}

删除集合项目

要删除集合中的项目,请使用 remove()discard() 方法。

remove()discard() 的区别是如果要删除的元素不存在 remove() 方法会引发错误,而 discard() 方法不会引发错误。

使用 remove() 方法删除“banana” :

thisset = {"apple", "banana", "cherry"}
thisset.remove("banana")
print(thisset) # {'cherry', 'apple'}

使用 discard() 方法删除“banana” :

thisset = {"apple", "banana", "cherry"}
thisset.discard("banana")
print(thisset) # {'cherry', 'apple'}

也可以使用 pop() 方法删除一个项目,此方法将删除 最后一个 项目。但是由于集合是无序的,因此在删除之前是不知道删除哪个项目的,删除后 pop() 的返回值返回的是删除的项目。

使用以下pop() 方法删除最后一项:

thisset = {"apple", "banana", "cherry"}
x = thisset.pop()

print(x)        # cherry
print(thisset)  # {'apple', 'banana'}

清空集合

clear() 方法清空集合。

thisset = {"apple", "banana", "cherry"}
thisset.clear()
print(thisset) # set()

del 关键字将完全删除该集合:

thisset = {"apple", "banana", "cherry"}
del thisset
print(thisset) # NameError: name 'thisset' is not defined

连接集合

在 Python 中有几种方法可以连接两个或多个集合。

可以使用 union() 方法返回包含两个集合中所有项的新集合的,也可以使用 update() 将一个集合中的所有项插入另一个集合中。

union() 方法返回一个包含两个集合中所有项目的新集合:

set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}

set3 = set1.union(set2)
print(set3)   # {1, 2, 3, 'c', 'a', 'b'}

update() 方法将 set2 中的项插入到 set1 中:

set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}

set1.update(set2)
print(set1)   # {1, 2, 3, 'c', 'a', 'b'}

!! **注意:**无论 union()update() 都删除任何重复的项目。

合并集合只保留重复项

intersection_update() 方法更新当前集合,保留两个集合中共存的项目。

x 集合中只保留和 y 集合中共存的项目:

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}

x.intersection_update(y)

print(x) # {'apple'}

intersection() 方法将返回一个新集合,该集合包含两个集合中共存的项目。

返回一个 x 集合和 y 集合中共存的项目的新集合:

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}

z = x.intersection(y)

print(z)

合并集合不保留重复项

symmetric_difference_update() 方法更新现有的集合,保留两个集合中不同的项目。

保留两个集合中都不存在的项目:

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}

x.symmetric_difference_update(y)
print(x) # {'cherry', 'google', 'banana', 'microsoft'}

symmetric_difference() 方法将返回一个新集合,该集合仅包含两个集合中都不存在的项目。

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}

z = x.symmetric_difference(y)
print(z) # {'cherry', 'google', 'banana', 'microsoft'}

集合方法

Python 有一组可以在集合上使用的内置方法。

方法 说明
add() 给当前集合添加一个项目
clear() 清空集合的所有元素
copy() 拷贝一个集合,返回一个集合的副本
difference() 返回当前集合中和另一个集合不同的项目组成的集合
difference_update() 更新当前集合,只留下和不存在于另外一个集合中的项目
discard() 删除指定的项目
intersection() 返回两个集合的并集
intersection_update() 更新当前集合,保留两个集合中共存的项目
isdisjoint() 两个集合是否又并集
issubset() 当前集合是否是另一个指定集合的子集
issuperset() 另一个指定集合是否是当前集合的子集
pop() 删除一个集合的项目
remove() 删除指定的项目,项目不存在报错
symmetric_difference() 返回一个新集合,该集合仅包含两个集合中都不存在的项目
symmetric_difference_update() 更新现有的集合,保留两个集合中不同的项目
union() 两个集合的合集
update() 更新当前集合,将指定集合的元素合并到当前集合