# 创建一个集合 (set)
my_set = {1, 2, 3, 4, 5}
print(my_set) # 输出: {1, 2, 3, 4, 5}
# 集合中的元素是唯一的,重复的元素会被自动去除
another_set = {1, 2, 2, 3, 4, 4, 5}
print(another_set) # 输出: {1, 2, 3, 4, 5}
# 添加元素到集合中
my_set.add(6)
print(my_set) # 输出: {1, 2, 3, 4, 5, 6}
# 移除集合中的元素
my_set.remove(3)
print(my_set) # 输出: {1, 2, 4, 5, 6}
# 集合的交集操作
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
intersection = set1.intersection(set2)
print(intersection) # 输出: {3, 4}
# 集合的并集操作
union = set1.union(set2)
print(union) # 输出: {1, 2, 3, 4, 5, 6}
# 集合的差集操作
difference = set1.difference(set2)
print(difference) # 输出: {1, 2}
add() 方法向集合中添加元素,使用 remove() 方法移除指定的元素。intersection)、并集(union)和差集(difference)。上一篇:python做界面
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站