字典
字典:简单讲就是无序的,可变的,可包含任意对象的“键值对”(key:value)的集合
特点:
1. 属于可变序列
2. 可变长度
3. 任意类型
4. 任意嵌套
5. 支持原位改变
定义字典
none_dict = {} #定义一个空字典
user_info_dict = {"name":"马小跳","age":17,"gender":"male","job":"学神"}
print(none_dict)
print(user_info_dict)
运行结果:
{}
{'name': '马小跳', 'age': 17, 'gender': 'male', 'job': '学神'}
字典的增删改查
修改:
user_info_dict = {"name":"马小跳","age":17,"gender":"male","job":"学神"}
print(user_info_dict)
user_info_dict["name"] = "马大哈" #将name的value替换为"马大哈"
print(user_info_dict)
```
运行结果:
{‘name’: ‘马小跳’, ‘age’: 17, ‘gender’: ‘male’, ‘job’: ‘学神’}
{‘name’: ‘马大哈’, ‘age’: 17, ‘gender’: ‘male’, ‘job’: ‘学神’}
###### 查询:
```python
user_info_dict = {"name":"马小跳","age":17,"gender":"male","job":"学神"}
print("%s的年龄是:%d,性别是:%s,工作:%s"%(user_info_dict["name"],user_info_dict["age"],user_info_dict["gender"],user_info_dict["job"]))
<div class="se-preview-section-delimiter"></div>
#key不能相同,相同后面的会覆盖前面的
user_info_dict = {"name":"马小跳","age":17,"gender":"male","job":"学神","job":"学炸"}
print("工作:%s"%user_info_dict["job"])
<div class="se-preview-section-delimiter"></div>
运行结果:
马小跳的年龄是:17,性别是:male,工作:学神
工作:学炸
<div class="se-preview-section-delimiter"></div>
添加:
user_info_dict = {"name":"马小跳","age":17,"gender":"male","job":"学神"}
user_info_dict["tel"] = "909272590"
print(user_info_dict)
<div class="se-preview-section-delimiter"></div>
运行结果:
{'name': '马小跳', 'age': 17, 'gender': 'male', 'job': '学神', 'tel': '909272590'}
<div class="se-preview-section-delimiter"></div>
删除:
user_info_dict = {"name":"马小跳","age":17,"gender":"male","job":"学神"}
del user_info_dict["job"]
print(user_info_dict)
<div class="se-preview-section-delimiter"></div>
运行结果:
{'name': '马小跳', 'age': 17, 'gender': 'male'}
<div class="se-preview-section-delimiter"></div>
字典.keys() 来获取字典中的所有的键
user_info_dict = {"name":"马小跳","age":17,"gender":"male","job":"学神"}
for key in user_info_dict.keys():
print(key)
<div class="se-preview-section-delimiter"></div>
运行结果:
name
age
gender
job
<div class="se-preview-section-delimiter"></div>
字典.values() 来获取字典中所有的值
user_info_dict = {"name":"马小跳","age":17,"gender":"male","job":"学神"}
for values in user_info_dict.values():
print(values)
<div class="se-preview-section-delimiter"></div>
运行结果:
马小跳
17
male
学神
<div class="se-preview-section-delimiter"></div>
.items() 获取字典中的某个键值对返回一个元组
将字典里的键值对输出成元组,输出最后一个元组的第一个元素
user_info_dict = {"name":"马小跳","age":17,"gender":"male","job":"学神"}
for item in user_info_dict.items():
print(item)
print(item[0])
<div class="se-preview-section-delimiter"></div>
运行结果:
('name', '马小跳')
('age', 17)
('gender', 'male')
('job', '学神')
job
<div class="se-preview-section-delimiter"></div>
如果需要把每个元素都遍历出来可以再添加一个临时变量
user_info_dict = {"name":"马小跳","age":17,"gender":"male","job":"学神"}
for key,value in user_info_dict.items():
print(key)
print(value)
<div class="se-preview-section-delimiter"></div>
运行结果:
name
马小跳
age
17
gender
male
job
学神
<div class="se-preview-section-delimiter"></div>
clear()清空字典
user_info_dict = {"name":"马小跳","age":17,"gender":"male","job":"学神"}
print(user_info_dict)
user_info_dict.clear()
print(user_info_dict)
<div class="se-preview-section-delimiter"></div>
运行结果:
{'name': '马小跳', 'age': 17, 'gender': 'male', 'job': '学神'}
{}
<div class="se-preview-section-delimiter"></div>
通过value 取 key
user_info_dict = {"name":"马小跳","age":17,"gender":"male","job":"学神"}
key_list = list(user_info_dict.keys()) #将key转换为列表
value_list = list(user_info_dict.values()) #将value转换为列表
print(key_list) #输出key列表
print(value_list) #输出value列表
result = key_list[value_list.index("学神")] #从value列表中查找"学神"的下标,再从key列表中查找以学神下标为下标的元素
print(result) #输出这个元素
<div class="se-preview-section-delimiter"></div>
运行结果 :
['name', 'age', 'gender', 'job']
['马小跳', 17, 'male', '学神']
job
<div class="se-preview-section-delimiter"></div>
从单个字典里取key
d = {"name":"悟空"}
key1,= d
print(key1)
<div class="se-preview-section-delimiter"></div>
运行结果 :
name
<div class="se-preview-section-delimiter"></div>
从多个字典中取key
注意:变量需要和字典中的key数量对应
d = {"name":"悟空","my":1,"you":2}
key1,key2,key3 = d
print(key1,key2,key3)
<div class="se-preview-section-delimiter"></div>
运行结果:
name my you