# 2.12 集合

集合（set）是一个无序且不含重复元素的序列。

集合主要用来进行成员关系测试和删除重复元素。

我们可以使用大括号“{}”或者set()函数来创建集合。

我们可以看到下图中，在集合定义的时候，集合中是有重复元素的，但是它自动去除了重复元素：

![图2-61](blob:https://minghuiwu.gitbook.io/8a62ead7-9676-44d0-8e03-418a0aba84eb)

我们也可以使用“in”来判断元素是否在集合中：

![图2-62](blob:https://minghuiwu.gitbook.io/7828efa7-852b-4743-8ecd-e2ed5db1a2e2)

集合也有许多的操作：交（&）、并（|）、差（-）、补（^）

![图2-63](blob:https://minghuiwu.gitbook.io/1050965a-0f88-4e53-a463-39e2fe71c912)

![图2-64](blob:https://minghuiwu.gitbook.io/2d634bba-2aa3-4833-9f5c-b14d7bc38f8c)

集合的补也可以用两个集合的并集减去两个集合的交集得到。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://minghuiwu.gitbook.io/tfbook/fu-lu-2-gong-yu-shan-qi-shi-bi-xian-li-qi-qi-jian-ming-python-ji-chu/fu-lu-1-gong-yu-shan-qi-shi-bi-xian-li-qi-qi-jian-ming-python-ji-chu/2.12-ji-he.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
