当前位置:首页>python>Python入门必看:集合 set 详解|去重神器

Python入门必看:集合 set 详解|去重神器

  • 2026-09-08 23:06:10
Python入门必看:集合 set 详解|去重神器
我是 Jerry,一个正在学 Python 的普通人。

前面写了列表、元组、字典,今天聊聊集合 —— 这玩意儿看起来不起眼,但用好了能省很多事儿。


一、集合到底是啥?

集合就是一个没顺序、不能重复的"东西"。

听起来有点绕?换个说法:

  • 列表:可以有重复元素,有顺序

  • 字典:键不能重复,值可以重复

  • 集合:所有元素都不能重复

# 列表,可以有重复my_list= [1, 2, 2, 3, 3, 3]print(my_list)  # [1, 2, 2, 3, 3, 3]# 集合,自动去重my_set= {1, 2, 2, 3, 3, 3}print(my_set)  # {1, 2, 3}

记住三点:

  • 不重复(自动去重)

  • 无顺序(不支持下标)

  • 只能是不可变类型(字符串、数字、元组)


二、怎么创建集合?

方式一:用花括号

# 直接创建fruits= {"苹果", "香蕉", "橙子"}print(fruits)  # {'苹果', '香蕉', '橙子'}# 用 set() 从列表转换nums= [1, 2, 2, 3, 3, 3]uq_nums=set(nums)print(uq_nums)  # {1, 2, 3}

方式二:用 set()

# 从字符串创建(去重)word="hello"letters=set(word)print(letters)  # {'h', 'e', 'l', 'o'}# 创建空集合(注意:不能用 {},那是空字典)empty_set=set()print(empty_set)  # set()

三、集合的常用操作

1. 添加元素

fruits= {"苹果", "香蕉"}# 添加一个fruits.add("橙子")print(fruits)  # {'苹果', '香蕉', '橙子'}# 添加多个(用 update)fruits.update({"葡萄", "西瓜"})print(fruits)  # {'苹果', '香蕉', '橙子', '葡萄', '西瓜'}

2. 删除元素

fruits= {"苹果", "香蕉", "橙子"}# remove() - 不存在会报错fruits.remove("香蕉")print(fruits)  # {'苹果', '橙子'}# discard() - 不存在不会报错(推荐)fruits.discard("葡萄")  # 不会报错print(fruits)  # {'苹果', '橙子'}# pop() - 随机删除一个fruits.pop()print(fruits)  # {'橙子'},删了一个,剩下一个# clear() - 清空所有fruits.clear()print(fruits)  # {}

3. 判断元素是否存在

fruits= {"苹果", "香蕉", "橙子"}print("苹果" in fruits)    # Trueprint("葡萄" in fruits)    # False

四、集合的运算(核心功能)

集合最牛的地方在于:做集合运算特别方便。

1. 并集:合并两个集合

set1= {1, 2, 3}set2= {3, 4, 5}# 方式一:|result = set1|set2print(result)  # {1, 2, 3, 4, 5}# 方式二:union()result = set1.union(set2)print(result)  # {1, 2, 3, 4, 5}

2. 交集:两个集合都有的

set1= {1, 2, 3}set2= {3, 4, 5}# 方式一:&result = set1&set2print(result)  # {3}# 方式二:intersection()result = set1.intersection(set2)print(result)  # {3}

3. 差集:A有但B没有的

set1= {1, 2, 3}set2= {3, 4, 5}# 方式一:-result = set1-set2print(result)  # {1, 2}# 方式二:difference()result = set1.difference(set2)print(result)  # {1, 2}

4. 对称差集:只在其中一个里有的

set1= {1, 2, 3}set2= {3, 4, 5}# 方式一:^result = set1^set2print(result)  # {1, 2, 4, 5}# 方式二:symmetric_difference()result = set1.symmetric_difference(set2)print(result)  # {1, 2, 4, 5}

五、实战:去重神器

集合最常用的场景就是去重。

案例1:列表去重

names= ["张三", "李四", "王五", "张三", "李四", "赵六"]# 用集合去重unique_names=list(set(names))print(unique_names)  # ['张三', '李四', '王五', '赵六']# 但顺序会变,如果要保持顺序,用其他方法# 保持顺序的去重:seen= []for name in names:if name not in seen:seen.append(name)print(seen)  # ['张三', '李四', '王五', '赵六']

案例2:找出两个列表的重复元素

list1= [1, 2, 3, 4, 5]list2= [4, 5, 6, 7, 8]# 用集合的交集common = set(list1) & set(list2)print(common)  # {4, 5}

案例3:找出两个列表的不同元素

list1= [1, 2, 3, 4, 5]list2= [4, 5, 6, 7, 8]# 用对称差集diff = set(list1) ^ set(list2)print(diff)  # {1, 2, 3, 6, 7, 8}

六、子集和超集

判断集合之间的关系:

set1= {1, 2, 3}set2= {1, 2, 3, 4, 5}set3= {1, 2, 3}# issubset() - 是否是子集print(set1.issubset(set2))   # True(1,2,3都在set2里)print(set2.issubset(set1))   # False# issuperset() - 是否是超集print(set2.issuperset(set1)) # True(set2包含set1的所有元素)print(set1.issuperset(set2)) # False

七、什么时候用集合?

场景用啥为啥
要去重集合自动去重,简单
判断"有没有"集合in 操作比列表快很多
做交集/并集/差集集合一行代码搞定
要保持顺序列表/字典集合无顺序
要存重复数据列表集合不行

最后说两句

集合这玩意儿,日常编程中使用频率很高。特别是:

  • 去重

  • 判断是否存在(比列表快很多)

  • 做集合运算(交集、并集、差集...)

学会之后会发现,很多原本写起来很麻烦的需求,用集合一行就搞定了。

今天的内容:

  • 集合创建:{} 或 set()

  • 添加删除:add/discard/remove/pop

  • 集合运算:| & - ^ 

  • 实战去重:列表去重、交集差集

  • 子集判断:issubset/issuperset

下篇打算再聊一聊字符串。


关注「AI 学习成长记」,我踩过的坑你别再踩。

最新文章

随机文章