三分钟掌握Python 中最常用的 10 种 Set 方法

liftword2个月前 (02-26)技术文章21

Python 中的集合至关重要,也是 Python 中最常用的内置数据类型之一。

集合具有一些主要属性。

  • 集合中的元素必须是唯一的。套装中不允许有重复项。
  • 它们是无序的
  • 设置项目不可更改,但您可以删除和添加新项目

可以使用两个选项创建集合。

  • 使用 set()
  • 带大括号 {}

创建一个带有函数的集合set()

employee_ids = set([1, 2, 3, 4, 5, 5])

print(employee_ids)

# {1, 2, 3, 4, 5}

在上面的例子中,写了两次 5,但当打印集合时可以看到没有重复——只有一个 5。

创建一个带有大括号的集合。

可以通过将逗号分隔的元素序列括在大括号内来直接创建集合。

employee_ids = {1, 2, 3, 4, 5, 5}

print(employee_ids)

# {1, 2, 3, 4, 5}

但是在这里必须小心,如果我们想创建一个空集合,应该使用函数set,因为空的大括号创建的是字典,而不是集合。

Python 中最常用的 10 种 Set 方法。

现在通过示例来发现最常用的集合方法。

  1. Add(元素)

将元素添加到集合中。

employee_ids = {1, 2, 3, 4, 5}

print(employee_ids)


employee_ids.add(6)
print(employee_ids)
# {1, 2, 3, 4, 5}
# {1, 2, 3, 4, 5, 6}

2. renmove(元素)

从集合中移除指定的元素。如果未找到该元素,则引发 KeyError

employee_ids = {1, 2, 3, 4, 5}

employee_ids.remove(3)
print(employee_ids)

# {1, 2, 4, 5}

employee_ids.remove(333)
# KeyError: 333

3. discard(元素)

从集合中删除指定的元素(如果存在)。如果未找到该元素,则不会引发错误。

employee_ids = {1, 2, 3, 4, 5}

print(employee_ids)


employee_ids.remove(3)
print(employee_ids)

# {1, 2, 3, 4, 5}

employee_ids.discard(333)
print(employee_ids)
# {1, 2, 3, 4, 5}

4. 并集

返回一个新集,其中包含其他集的项的唯一列表。

employee_ids_part1 = {1, 2, 3, 4, 5}
employee_ids_part2 = {4, 5, 6, 7, 8}
employee_ids_part3 = {7, 8, 9, 10, 11}
full = employee_ids_part1.union(employee_ids_part2, employee_ids_part3)
print(full)
# {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
  1. 交集

返回一个新集合,其中包含两个集合的公共元素。

cs_courses = {'History', 'Math', 'Physics', 'CompSci'}
art_courses = {'History', 'Math', 'Art', 'Design'}

common_courses = cs_courses.intersection(art_courses)
print(common_courses)

# {'Math', 'History'}
  1. 集合差

返回一个新集,其中包含调不在指定集中的元素。

cs_courses = {'History', 'Math', 'Physics', 'CompSci'}
art_courses = {'History', 'Math', 'Art', 'Design'}

difference = cs_courses.difference(art_courses)
print(difference)

# {'Physics', 'CompSci'}

7. 对称差

返回一个新集,其中包含每个集唯一的元素。当想要处理集合之间的差异并且不关心公共元素时,对称差异非常有用。在处理配置、选项或首选项时,通常会出现这种情况。

user_preferences1 = {'theme', 'language', 'fontSize'}
user_preferences2 = {'language', 'fontSize', 'timezone'}
difference_preferences = user_preferences1.symmetric_difference(user_preferences2)
print(difference_preferences)
# {'theme', 'timezone'}
  1. 判断是否为子集

如果给定集和是另一个集合的子集,则此方法返回 True。


current_users = {'john', 'alice', 'bob', 'mary'}
authorized_users = {'john', 'alice', 'dave', 'mary', 'mike', 'bob', 'susan'}


is_everyone_authorized = current_users.issubset(authorized_users)

print(is_everyone_authorized)
# True
  1. 复制

返回给定集合的浅表副本。

original_set = {1, 2, 3}
copied_set = original_set.copy()
# Result: {1, 2, 3}

10. 判断两个集合是否有相同元素)

返回 True:如果两个集合不相交,则表示给定集合之间没有公共元素。

set1 = {1, 2, 3}
set2 = {4, 5, 6}

result = set1.isdisjoint(set2)
print(result)
# Result: True

set3 = {1, 2, 3}
set4 = {3, 4, 5}

result_new = set3.isdisjoint(set4)
print(result_new)
# Result: False

相关文章

Python 集合(Set)的十个经典案例_python 集合 discard

Python 集合(Set)是一种无序、可变的数据结构,存储不重复的元素。集合类似于数学上的集合概念,可以进行交集、并集、差集等运算。1. 创建集合可以使用花括号 {} 或 set() 函数创建集合。...

神奇的 Python set():如何让重复元素无处可藏

前言如果你还没认真研究过 Python 中的集合(set)类型,那你可真是错失了一项神器!集合不仅能让你的代码变得更高效,还能避免一堆重复数据让你头疼。每当你用 Python 编程时,数据就像是无序的...

Python基础之Set集合操作_python set取并集

''' 集合是无序和无索引的集合。在 Python 中,集合用花括号编写。 集合set,元素是不可重复的 ''' # 创建集合 demoSet = {'a', 'b', 'c'} p...

Python基础篇(三)dict和set类型_python dicts

dict一、dict的定义dict即字典,相当于golang中的map类型,key-value型.具有极快的查找速度.由于dict中key的值是唯一的,因此如果多次赋值,最终结果为最后一次的赋值.二、...

简单学Python——内置函数24——set()函数

set()函数是Python的内置函数,用来创建集合数据(集合是一种无序且不含重复元素的数据结构)。set()函数的参数为一个可迭代对象(列表、字符串等)1、创建非空集合。用已有列表创建集合:set(...

Python基础知识之集合(set)中交集、并集、差集运算

集合中最常见的操作就是并集、交集和差集,为了更好地学习这些知识和编程实现方式。我们从并集、交集和差集各运算的含义开始讲解。并集:取两集合全部的元素。交集:取两集合公共的元素。差集:取一个集合中另一集合...