### 二、字典推导式
---
思考:如果有如下两个列表:
````python
list1 = ['name','age','gender']
list2 = ['Tom',20,'man']
````
如何快速合并为一个字典?
答:字典推导式
字典推导式作用:快速合并为列表为字典或提取字典中目标数据。
### 2.1快速体验
---
1.创建一个字典:字典key是1-5数字,value是这个数字的二次方。
```python
# 创建字典 key是1-5的数字,v是这个数字的平方
dict1 = {}
for i in range(1,5):
dict1[i] = i**2
print(dict1)
dict1 = {i: i**2 for i in range(1,5)}
print(dict1)
```
2.将两个列表合并为一个字典
```python
list1 = ['name','age','gender']
list2 = ['Tom',20,'man']
# dict1 = {}
#
# for i in range(len(list1)):
# dict1[list1[i]] = list2[i]
#
# print(dict1)
dict1 = {list1[i]: list2[i] for i in range(len(list1))}
print(dict1)
```
3.提取字典中的目标数据
```python
counts = {'MBP':268,'HP':125,'DELL':201,'Lenovo':199,'acer':99}
# 需求:提取上述电脑数量大于等于200的字典数据
# counts1 = {}
#
# for key,value in counts.items():
# if value >= 200:
# counts1[key] = value
#
# print(counts1)
counts1 = {key: value for key,value in counts.items() if value >= 200}
print(counts1)
# {'MBP': 268, 'DELL': 201}
```