# 集合中的交集、并集与差集特性

``````s1 = {1, 2, 3}
s2 = {1, 4, 5}
print(s1 & s2)
print(s1 | s2)
print(s1 - s2)
print(s2 - s1)
``````

# 数据序列中的公共方法

## 第一部分

+ 合并 字符串、列表、元组
* 复制 字符串、列表、元组
in 元素是否存在 字符串、列表、元组、字典
not in 元素是否不存在 字符串、列表、元组、字典

``````l1 = [1, 2, 3]
l2 = [4, 5, 3]
print(l1 + l2)

s1 = "123"
s2 = "456"
print(s1 + s2)

t1 = (1, 2, 3)
t2 = (4, 5, 6)
print(t1 + t2)
``````

``````print('-' * 10)
print([1] * 10)
print((10, ) * 10)
``````

## 第二部分

1 len() 计算容器中元素个数
2 del或del() 根据索引下标删除指定元素
3 max() 返回容器中元素最大值
4 min() 返回容器中元素最小值
5 range(start, end, step) 生成从start到end（不包含）的数字，步长为 step，供for循环使用
6 enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列，同时列出数据和数据下标，一般用在 for 循环当中。

``````d1 = {"1": 1, "2": 2, "3": 3}
del d1["1"]
print(d1)

l1 = [1, 2, 3]
del l1[1]
print(l1)
``````

## 序列类型之间的相互转换

### list()：把某个序列类型的数据转化为列表

``````t = (1, 2, 3)
print(list(t))

s = {1, 2, 3}
print(list(s))

d = {"3": 1, "2": 2, "1": 3}
print(list(d))
``````

### tuple()：把某个序列类型的数据转化为元组

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

s = {1, 2, 3}
print(tuple(s))
``````

### set()：将某个序列转换成集合（集合可以快速完成列表去重并且不支持下标）

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

t = (1, 2, 3, 3)
print(set(t))
``````

# 列表集合字典推导式

## 列表推导式

``````变量名 = [表达式 for 变量 in 列表 for 变量 in 列表]

``````
``````l1 = [f"{i}--{j}" for i in range(1, 3) for j in range(4, 6)]
print(l1)
l1_ = []
for i in range(1, 3):
for j in range(4, 6):
l1_.append(f"{i}--{j}")
print(l1_)

l2 = [i for i in range(1, 10) if i % 2 == 0]
print(l2)
l2_ = []
for i in range(1, 10):
if i % 2 == 0:
l2_.append(i)
print(l2_)
``````

## 字典推导式

``````dict1 = {i:i**2 for i in range(1,6)}
print(dict1)
``````

``````list1 = ['name', 'age', 'gender']
list2 = ['Tom', 20, 'male']

person = {list1[i]:list2[i] for i in range(len(list1))}
print(person)
``````

``````counts = {'MBP': 268, 'HP': 125, 'DELL': 201, 'Lenovo': 199, 'ACER': 99}

# 需求：提取上述电脑数量大于等于200的字典数据
counts = {key:value for key, value in counts.items() if value >= 200}
print(counts)
``````

## 集合推导式

``````l1 = [1, 1, 2]
l = [i ** 2 for i in l1]
print(l)
s = {i ** 2 for i in l1}
print(s)
``````

THE END