---
title: "Python set"
author: "fairy_xzc"
date: "2016-09-29T08:55:06Z"
tags: []
categories: ["编程语言"]
slug: "python-set"
draft: false
lastmod: "2016-09-29T08:58:33Z"
id: 2327559
url: "https://agilestyle.iteye.com/blog/2327559"
---

**原创装载请注明出处：[http://agilestyle.iteye.com/blog/2327559](/blog/2327559)**

 

set.py

```
l = [1, 3, 2, 1, 2, 3]
print(l)
s = set(l)
print(s)

# add(key)方法可以添加元素到set中，可以重复添加，但不会有效果
s.add(4)
print(s)

# remove(key)方法可以删除元素
s.remove(4)
print(s)

s1 = set([1, 2, 3])
s2 = set([2, 3, 4])
# {2, 3}
print(s1 & s2)
# {1, 2, 3, 4}
print(s1 | s2)
```

 

Console Output

![](http://dl2.iteye.com/upload/attachment/0120/2877/b0ece34c-0734-3142-bd29-7d714f89e9d1.png)