第28集:集合基础操作

本集学习目标

  • 理解集合的概念和特点
  • 掌握集合的创建方法
  • 学会集合的基本操作
  • 掌握集合的数学运算
  • 理解集合的去重特性
  • 通过实际练习掌握集合的使用

什么是集合?

集合(Set)是Python中用于存储不重复元素的无序集合。

集合的特点

  1. 使用花括号 {} 创建
  2. 元素之间用逗号 , 分隔
  3. 集合是无序
  4. 集合中的元素不重复
  5. 元素必须是不可变类型
  6. 集合是可变的(可以添加删除)

创建集合

基本创建方式

# 空集合(注意:不是{},而是set())
set1 = set()

# 创建集合
set2 = {1, 2, 3, 4, 5}
set3 = {"apple", "banana", "orange"}
set4 = {1, "hello", 3.14}  # 混合类型

注意:{}创建的是字典

# {}创建的是空字典,不是空集合
empty = {}
print(type(empty))  # <class 'dict'>

# 创建空集合要使用set()
empty_set = set()
print(type(empty_set))  # <class 'set'>

使用set()函数

# 从列表创建集合(自动去重)
list1 = [1, 2, 2, 3, 3, 3]
set1 = set(list1)
print(set1)  # {1, 2, 3}

# 从字符串创建集合
set2 = set("hello")
print(set2)  # {'h', 'e', 'l', 'o'} (顺序可能不同)

集合的基本操作

添加元素 - add()

fruits = {"apple", "banana"}

fruits.add("orange")
print(fruits)  # {'apple', 'banana', 'orange'}

添加多个元素 - update()

fruits = {"apple", "banana"}

fruits.update(["orange", "grape"])
print(fruits)  # {'apple', 'banana', 'orange', 'grape'}

删除元素 - remove()

删除指定元素,元素不存在会报错。

fruits = {"apple", "banana", "orange"}

fruits.remove("banana")
print(fruits)  # {'apple', 'orange'}

# fruits.remove("pear")  # KeyError

删除元素 - discard()

删除指定元素,元素不存在不会报错。

fruits = {"apple", "banana", "orange"}

fruits.discard("banana")
print(fruits)  # {'apple', 'orange'}

fruits.discard("pear")  # 不会报错

随机删除并返回 - pop()

fruits = {"apple", "banana", "orange"}

item = fruits.pop()
print(item)    # 随机删除一个元素
print(fruits)   # 剩余元素

清空集合 - clear()

fruits = {"apple", "banana", "orange"}

fruits.clear()
print(fruits)  # set()

集合的数学运算

并集 - union() 或 |

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

result = set1.union(set2)
print(result)  # {1, 2, 3, 4, 5}

# 或使用运算符
result = set1 | set2
print(result)  # {1, 2, 3, 4, 5}

交集 - intersection() 或 &

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

result = set1.intersection(set2)
print(result)  # {3}

# 或使用运算符
result = set1 & set2
print(result)  # {3}

差集 - difference() 或 -

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

result = set1.difference(set2)
print(result)  # {1, 2}

# 或使用运算符
result = set1 - set2
print(result)  # {1, 2}

对称差集 - symmetric_difference() 或 ^

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

result = set1.symmetric_difference(set2)
print(result)  # {1, 2, 4, 5}

# 或使用运算符
result = set1 ^ set2
print(result)  # {1, 2, 4, 5}

集合的其他方法

长度 - len()

fruits = {"apple", "banana", "orange"}

print(len(fruits))  # 3

检查元素是否存在

fruits = {"apple", "banana", "orange"}

print("apple" in fruits)   # True
print("grape" in fruits)   # False

去重

# 去除列表中的重复元素
numbers = [1, 2, 2, 3, 3, 3]
unique_numbers = list(set(numbers))
print(unique_numbers)  # [1, 2, 3] (顺序可能不同)

实际应用案例

案例1:找出两个列表的共同元素

list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]

set1 = set(list1)
set2 = set(list2)

common = set1 & set2
print(common)  # {4, 5}

案例2:去重

words = ["apple", "banana", "apple", "orange", "banana"]
unique_words = list(set(words))
print(unique_words)

本集小结

  1. 集合存储不重复元素
  2. 使用花括号 {} 创建
  3. 集合是无序
  4. 元素不重复
  5. add():添加元素
  6. remove()discard():删除元素
  7. 并集:union()|
  8. 交集:intersection()&amp;
  9. 差集:difference()-
  10. 常用于去重

课后练习

练习1:创建集合

创建包含数字1-10的集合。

练习2:集合运算

求两个集合的并集、交集、差集。

练习3:去重

对列表[1,2,2,3,3,3,4,4,4,4]去重。

练习4:添加删除

向集合添加元素,然后删除元素。

练习5:共同元素

找出两个列表的共同元素。

下集预告

下一集我们将学习第29集:数据类型转换。Python中各种数据类型之间的转换是非常常见的操作。

敬请期待!

« 上一篇 字典常用方法 下一篇 » 数据类型转换