目录
一、定义
用于表示一组无序的唯一元素,可以使用花括号或 set() 函数来定义集合。
特点:-
集合中的元素是无序的,不能通过索引访问。-
集合中的元素是唯一的,不会重复。
my_set = {1, 2, 3, 4, 5}
print(my_set) # {1, 2, 3, 4, 5}
my_set2 = set([1, 2, 2, 3, 3, 4, 5, 5])
print(my_set2) # {1, 2, 3, 4, 5}
my_set3 = set((1, 2, 2, 3, 3, 4, 5, 5))
print(my_set3) # {1, 2, 3, 4, 5}
二、相关操作
1、添加元素
1.1、add
向集合中添加单个元素。
my_set = {1, 2, 3}
my_set.add(4)
print(my_set) # {1, 2, 3, 4}
1.2、update
向集合中添加多个元素。
my_set = {1, 2, 3}
my_set.update([4, 5, 6])
print(my_set) # {1, 2, 3, 4, 5, 6}
2、删除元素
2.1、remove
删除集合中的指定元素,元素不存在时会报错。
my_set = {1, 2, 3, 4, 5}
my_set.remove(3)
print(my_set) # {1, 2, 4, 5}
my_set.remove(6)
print(my_set) # 报错:KeyError: 6
2.2、pop
随机移除集合中的一个元素,并返回该元素。由于集合是无序的,无法预测将被移除的元素是哪个。
my_set = {1, 2, 3, 4, 5}
removed_element = my_set.pop()
print(removed_element) # 输出:随机移除的一个元素
print(my_set) # 输出:移除元素后的集合
2.3、discard
删除集合中的指定元素,元素不存在,不会引发错误。
my_set = {1, 2, 3, 4, 5}
my_set.discard(3)
print(my_set) # {1, 2, 4, 5}
my_set.discard(6) # 元素不存在,不会引发错误
print(my_set) # {1, 2, 4, 5}
2.4、clear
清空集合中的所有元素。
set1 = {1, 2, 3}
set1.clear()
print(set1) # set()
3、len
获取集合中元素的个数。
set1 = {1, 2, 3, 4, 5}
print(len(set1)) # 5
set2 = set()
print(len(set2)) # 0
三、交集、并集和差集
1、交集
使用 intersection() 方法或 & 运算符,返回两个集合中共有的元素组成的新集合。
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
# 使用 intersection() 方法获取两个集合的交集
intersection_set = set1.intersection(set2)
print(intersection_set) # {3, 4}
# 使用 & 运算符获取两个集合的交集
intersection_set = set1 & set2
print(intersection_set) # {3, 4}
2、并集
使用 union() 方法或 | 运算符,返回包含两个集合中所有元素的新集合。
set1 = {1, 2, 3}
set2 = {3, 4, 5}
# 使用 union() 方法获取两个集合的并集
union_set = set1.union(set2)
print(union_set) # {1, 2, 3, 4, 5}
# 使用 | 运算符获取两个集合的并集
union_set = set1 | set2
print(union_set) # {1, 2, 3, 4, 5}
3、差集
使用 difference() 方法或 - 运算符,返回存在于第一个集合但不存在于第二个集合的元素组成的新集合。
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
# 使用 difference() 方法获取两个集合的差集
difference_set = set1.difference(set2)
print(difference_set) # {1, 2}
# 使用 - 运算符获取两个集合的差集
difference_set = set1 - set2
print(difference_set) # {1, 2}
4、对称差集
使用 symmetric_difference() 方法或 ^ 运算符获取两个集合的对称差集,即两个集合中不重复的元素组成的新集合。
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
# 使用 symmetric_difference() 方法获取两个集合的对称差集
symmetric_difference_set = set1.symmetric_difference(set2)
print(symmetric_difference_set) # {1, 2, 5, 6}
# 使用 ^ 运算符获取两个集合的对称差集
symmetric_difference_set = set1 ^ set2
print(symmetric_difference_set) # {1, 2, 5, 6}
5、子集判断
使用 issubset() 方法或 <= 运算符判断一个集合是否是另一个集合的子集。
set1 = {1, 2, 3}
set2 = {1, 2, 3, 4, 5}
# 使用 issubset() 方法判断 set1 是否是 set2 的子集
is_subset = set1.issubset(set2)
print(is_subset) # True
# 使用 <= 运算符判断 set1 是否是 set2 的子集
is_subset = set1 <= set2
print(is_subset) # True
6、超集判断
使用 issuperset() 方法或 >= 运算符判断一个集合是否是另一个集合的超集。
set1 = {1, 2, 3, 4, 5}
set2 = {1, 2, 3}
# 使用 issuperset() 方法判断 set1 是否是 set2 的超集
is_superset = set1.issuperset(set2)
print(is_superset) # True
# 使用 >= 运算符判断 set1 是否是 set2 的超集
is_superset = set1 >= set2
print(is_superset) # True
7、交集更新
使用 intersection_update() 方法,更新集合,使其只包含当前集合与另一个集合的交集部分。
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}
set1.intersection_update(set2)
print(set1) # {4, 5}
8、差集更新
使用 difference_update() 方法,更新集合,使其只包含当前集合与另一个集合的差异部分。
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}
set1.difference_update(set2)
print(set1) # {1, 2, 3}
9、对称差集更新
使用 symmetric_difference_update() 方法,更新集合,使其只包含当前集合与另一个集合的对称差集部分。
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}
set1.symmetric_difference_update(set2)
print(set1) # {1, 2, 3, 6, 7, 8}
10、判断是否不相交
使用 isdisjoint() 方法,判断两个集合是否没有共同的元素。
set1 = {1, 2, 3}
set2 = {4, 5, 6}
result = set1.isdisjoint(set2)
print(result) # True
四、集合推导式
在 Python 中,集合推导式(Set Comprehension)是一种快速创建新集合的方法。它允许你使用简洁的语法从可迭代对象中生成集合。
基本形式:
{表达式 for 变量 in 可迭代对象} 或者 {表达式 for 变量 in 可迭代对象 if 条件}
1、创建一个集合,包含数字列表中所有的偶数
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = {num for num in numbers if num % 2 == 0}
print(even_numbers) # {2, 4, 6, 8, 10}
2、创建一个集合,包含字符串列表中每个字符串的长度
strings = ['apple', 'banana', 'cherry', 'date']
string_lengths = {len(string) for string in strings}
print(string_lengths) # {4, 5, 6}
3、创建一个集合,包含两个集合的交集元素
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}
intersection = {x for x in set1 if x in set2}
print(intersection) # {4, 5}
4、将字符串列表中的每个字符串转换为小写,并创建一个包含所有小写字符串的集合
strings = ['Apple', 'Banana', 'Cherry', 'Date']
lowercase_set = {string.lower() for string in strings}
print(lowercase_set) # {'date', 'apple', 'banana', 'cherry'}
5、从一个嵌套列表中创建一个扁平化的集合,包含所有元素
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flattened_set = {item for sublist in nested_list for item in sublist}
print(flattened_set) # {1, 2, 3, 4, 5, 6, 7, 8, 9}
6、创建一个包含从1到10之间所有奇数的集合
odd_set = {x for x in range(1, 11) if x % 2 != 0}
print(odd_set) # {1, 3, 5, 7, 9}
7、从字典中提取所有的键并创建一个集合
my_dict = {'a': 1, 'b': 2, 'c': 3}
key_set = {key for key in my_dict}
print(key_set) # {'a', 'c', 'b'}
8、创建一个集合,包含字符串列表中每个字符串的首字母
strings = ['apple', 'banana', 'cherry', 'date']
first_letters = {string[0] for string in strings}
print(first_letters) # {'a', 'c', 'b', 'd'}
9、使用条件筛选,创建一个包含范围内能被3整除的数的集合
numbers = range(1, 10)
divisible_by_3 = {num for num in numbers if num % 3 == 0}
print(divisible_by_3) # {9, 3, 6}
10、创建一个包含两个集合的并集
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = {x for x in set1} | {x for x in set2}
print(union_set) # {1, 2, 3, 4, 5}
11、从列表中创建一个包含所有元素的唯一值集合
my_list = [1, 2, 3, 2, 4, 5, 3, 6]
unique_set = {x for x in my_list}
print(unique_set) # {1, 2, 3, 4, 5, 6}
12、创建一个包含字符串列表中每个字符串的逆序的集合
strings = ['apple', 'banana', 'cherry', 'date']
reversed_set = {string[::-1] for string in strings}
print(reversed_set) # {'etad', 'ananab', 'yrrehc', 'elppa'}
13、从字典中提取所有的值并创建一个集合
my_dict = {'a': 1, 'b': 2, 'c': 3}
value_set = {value for value in my_dict.values()}
print(value_set) # {1, 2, 3}
五、使用 for 循环遍历
my_set = {'a', 'b', 'c'}
for i in my_set:
print(i)
# 输出元素顺序是随机的
# c
# a
# b
六、enumerate 枚举
my_set = {'a', 'b', 'c'}
for index, value in enumerate(my_set):
print(index, value)
# 输出元素顺序是随机的
# 0 b
# 1 a
# 2 c
七、使用 while 循环
要使用 while 循环遍历集合,可以将集合转换为迭代器,并使用 while 循环配合 next() 函数来逐个获取集合中的元素。-
由于集合是无序的数据结构,每次遍历的顺序可能会有所不同。此外,当使用 while 循环遍历集合时,需要注意在适当的时候终止循环,以避免无限循环。
my_set = {'a', 'b', 'c'}
iterator = iter(my_set)
# 使用 while 循环遍历集合
while True:
try:
value = next(iterator)
print(value)
except StopIteration:
break
# 输出元素顺序是随机的
# a
# c
# b
八、应用
1、当有一群人参加派对时,统计有多少人的名字中包含字母"A"。
# 参加派对的人员名单
guests = ["Alice", "Bob", "Charlie", "David", "Eve", "Frank", "Grace"]
# 创建一个空集合用于存储名字中包含字母"A"的人员
name_set = set()
# 遍历参加派对的人员名单
for guest in guests:
# 判断名字中是否包含字母"A",如果包含,则将其添加到集合中
if "A" in guest.upper():
name_set.add(guest)
# 打印名字中包含字母"A"的人员名单
print("名字中包含字母'A'的人员:")
for name in name_set:
print(name)
# 名字中包含字母'A'的人员:
# Grace
# Alice
# David
# Frank
# Charlie
2、两个班级的学生名单,打印出同时在两个班级的学生名单。
# 两个班级的学生名单
class_a = {"Alice", "Bob", "Charlie", "David", "Eve"}
class_b = {"David", "Frank", "Grace", "Helen", "Alice"}
# 计算同时在两个班级的学生集合
common_students = class_a & class_b
# 打印同时在两个班级的学生名单
print("同时在两个班级的学生:")
for student in common_students:
print(student)
# 同时在两个班级的学生:
# David
# Alice
📝结尾
看到这里了还不给博主扣个:- ⛳️ 点赞☀️收藏 ⭐️ 关注!- 💛 💙 💜 ❤️ 💚💓 💗 💕 💞 💘 💖- 拜托拜托这个真的很重要!- 你们的点赞就是博主更新最大的动力!- 有问题可以评论或者私信呢秒回哦。