标题：Python - 集合

• 该集合中的元素不能重复。
• 该集合中的元素是不可变的（不能被修改），但集合作为一个整体是可变的。
• 没有索引附加到python集合中的任何元素。所以他们不支持任何索引或切片操作。

设置操作

python中的集合通常用于像union，intersection，difference和complement等数学运算。我们可以创建一个集合，访问它的元素并执行这些数学运算，如下所示。

创建一个集合

```Days=set(["Mon","Tue","Wed","Thu","Fri","Sat","Sun"])
Months={"Jan","Feb","Mar"}
Dates={21,22,17}
print(Days)
print(Months)
print(Dates)
```

```set(['Wed', 'Sun', 'Fri', 'Tue', 'Mon', 'Thu', 'Sat'])
set(['Jan', 'Mar', 'Feb'])
set([17, 21, 22])
```

访问集合中的值

```Days=set(["Mon","Tue","Wed","Thu","Fri","Sat","Sun"])

for d in Days:
print(d)
```

```Wed
Sun
Fri
Tue
Mon
Thu
Sat
```

将项目添加到集合

```Days=set(["Mon","Tue","Wed","Thu","Fri","Sat"])

print(Days)
```

```set(['Wed', 'Sun', 'Fri', 'Tue', 'Mon', 'Thu', 'Sat'])
```

从集合中删除项目

```Days=set(["Mon","Tue","Wed","Thu","Fri","Sat"])

print(Days)
```

```set(['Wed', 'Fri', 'Tue', 'Mon', 'Thu', 'Sat'])
```

集合

```DaysA = set(["Mon","Tue","Wed"])
DaysB = set(["Wed","Thu","Fri","Sat","Sun"])
AllDays = DaysA|DaysB
print(AllDays)
```

```set(['Wed', 'Fri', 'Tue', 'Mon', 'Thu', 'Sat'])
```

集合的交集

```DaysA = set(["Mon","Tue","Wed"])
DaysB = set(["Wed","Thu","Fri","Sat","Sun"])
AllDays = DaysA & DaysB
print(AllDays)
```

```set(['Wed'])
```

集合的差异

```DaysA = set(["Mon","Tue","Wed"])
DaysB = set(["Wed","Thu","Fri","Sat","Sun"])
AllDays = DaysA - DaysB
print(AllDays)
```

```set(['Mon', 'Tue'])
```

比较集合

```DaysA = set(["Mon","Tue","Wed"])
DaysB = set(["Mon","Tue","Wed","Thu","Fri","Sat","Sun"])
SubsetRes = DaysA <= DaysB
SupersetRes = DaysB >= DaysA
print(SubsetRes)
print(SupersetRes)
```

```True
True
```