0Pricing
Python For Kids · 课时

集合

了解由不重复元素组成的集合。

集合 是 CoddyKit 上的免费 Python For Kids 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Python For Kids 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Python For Kids 课程共包含 4 节课。

集合简介

集合

欢迎学习数据结构!今天我们将学习 Python 中的集合,它可以帮助您存储不重复的元素集合。

集合 — 插图 1

什么是集合?

集合是不重复项目的集合。与列表不同,集合不允许包含重复元素。当您需要确保所有项目都不重复时,集合非常有用。

  • 元素唯一:集合中不允许有重复项。
  • 无序:集合不会保留元素的顺序。

创建集合

您可以将项目放在花括号({})中,并用逗号分隔来创建集合,也可以使用 set() 函数。

示例:

# Creates a set of fruits using curly braces
fruits = {"apple", "banana", "cherry"}

# Creates a set of numbers using the set() function
numbers = set([1, 2, 3, 4, 5])

访问集合中的项目

由于集合是无序的,您无法通过索引访问其中的项目。不过,您可以使用循环遍历集合。

示例:

# Iterates through each fruit in the set and prints it
for fruit in fruits:
    print(fruit)

向集合中添加项目

您可以使用 add() 方法向集合中添加项目。

示例:

# Adds a new fruit to the set
fruits.add("orange")

# Attempts to add a duplicate fruit (will have no effect)
fruits.add("apple")

# Prints the updated set
print(fruits)  # Output might vary due to unordered nature

从集合中删除项目

您可以使用 remove() 或 discard() 方法从集合中删除项目。

示例:

# Removes 'banana' from the set
fruits.remove("banana")

# Removes 'grape' if it exists; does nothing otherwise
fruits.discard("grape")

# Prints the updated set
print(fruits)  # Output might vary

集合运算

集合支持并集、交集和差集等多种运算,您可以使用这些运算来合并或比较集合。

  • 并集:合并两个集合中的所有不重复元素。
  • 交集:获取两个集合中共有的元素。
  • 差集:获取存在于一个集合中但不存在于另一个集合中的元素。

示例:

# Creates two sets of numbers
set_a = {1, 2, 3, 4}
set_b = {3, 4, 5, 6}

# Union of set_a and set_b
union_set = set_a.union(set_b)
print("Union:", union_set)  # Output: {1, 2, 3, 4, 5, 6}

# Intersection of set_a and set_b
intersection_set = set_a.intersection(set_b)
print("Intersection:", intersection_set)  # Output: {3, 4}

# Difference of set_a from set_b
difference_set = set_a.difference(set_b)
print("Difference:", difference_set)  # Output: {1, 2}

示例程序:不重复的访问者

让我们使用集合创建一个记录网站不重复访问者的程序。

代码:

# Initializes an empty set to store unique visitor names
unique_visitors = set()

# List of visitors (including duplicates)
visitors = ["Alice", "Bob", "Charlie", "Alice", "Diana", "Bob"]

# Adds each visitor to the set
for visitor in visitors:
    unique_visitors.add(visitor)  # Adds the visitor name to the set

# Prints the total number of unique visitors
print("Total Unique Visitors:", len(unique_visitors))  # Output: 4

# Prints the names of unique visitors
print("Unique Visitors:", unique_visitors)  # Output: {'Alice', 'Bob', 'Charlie', 'Diana'}
# Creates a set of numbers
numbers = {1, 2, 3, 4, 4, 5}

# Adds a new number to the set
numbers.add(6)

# Attempts to add a duplicate number
numbers.add(2)

# Prints the set
print(numbers)

做得很好!

您已经学会了如何在 Python 中使用集合存储不重复的元素集合。当数据的唯一性很重要时,集合是管理数据的强大工具。请继续练习:创建您自己的集合,并尝试使用它们的各种运算!

集合 — 插图 10

常见问题解答

「集合」课时是免费的吗?

是的 — 「集合」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Python For Kids 课程的其余内容,请升级到 CoddyKit PRO。 Python For Kids 课程共包含 4 节课。

「集合」这节课中我会学到什么?

了解由不重复元素组成的集合。 你通过在浏览器中直接运行的动手代码来练习 Python For Kids,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Python For Kids 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Python For Kids 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「集合」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Python For Kids 课中编写并运行代码吗?

能。每节 Python For Kids 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 列表与元组
  2. 字典
  3. 集合
  4. 操作数据结构
← 返回 Python For Kids