Python自动化开发-数据类型之字典

Python 数据类型-字典

字典是Python重要的数据类型之一。接下来主要介绍字典数据类型的操作。

注意:前方高能!字典是无序的!!!

1.创建字典

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#字典:创建字典
dic01={"name":"Li chen","age":"36","job":"IT engineer","hobby":"basketball","hometown":"Liaoning Province"}
print(type(dic01))
print(dic01)
View Code

代码运行结果:

<class 'dict'>
{'hometown': 'Liaoning Province', 'name': 'Li chen', 'job': 'IT engineer', 'hobby': 'basketball', 'age': '36'}

2.字典:字典的增加

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#字典:通过键值对直接增加
dic01={"name":"Li chen","age":"36","job":"IT engineer","hobby":"basketball","hometown":"Liaoning Province"}
dic01["male"]=""
print(dic01)
View Code

代码运行结果:

{'job': 'IT engineer', 'male': '男', 'age': '36', 'hobby': 'basketball', 'name': 'Li chen', 'hometown': 'Liaoning Province'}

3.字典:字典的删除

通过KEY删除字典的键值对。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#字典:删除指定的KEY,有返回值,可以设置返回值
dic01={"name":"Li chen","age":"36","job":"IT engineer","hobby":"basketball","hometown":"Liaoning Province"}
r=dic01.pop("age")
print(r,dic01)
View Code

代码运行结果:

36 {'hobby': 'basketball', 'hometown': 'Liaoning Province', 'job': 'IT engineer', 'name': 'Li chen'}

清空整个字典。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#字典:清空整个字典
dic01={"name":"Li chen","age":"36","job":"IT engineer","hobby":"basketball","hometown":"Liaoning Province"}
print(dic01)
dic01.clear()
print(dic01)
View Code

代码运行结果:

{'hobby': 'basketball', 'name': 'Li chen', 'hometown': 'Liaoning Province', 'age': '36', 'job': 'IT engineer'}
{}

4.字典:字典的修改

通过键值对直接修改

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#字典:通过键值对直接修改
dic01={"name":"Li chen","age":"36","job":"IT engineer","hobby":"basketball","hometown":"Liaoning Province"}
print(dic01)
dic01["name"]="Wang yang"
print(dic01)
View Code

代码运行结果: 

{'hometown': 'Liaoning Province', 'name': 'Li chen', 'age': '36', 'hobby': 'basketball', 'job': 'IT engineer'}
{'hometown': 'Liaoning Province', 'name': 'Wang yang', 'age': '36', 'hobby': 'basketball', 'job': 'IT engineer'}

通过update修改:增加字典元素值

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#字典:通过update增加字典元素值
dic01={"name":"Li chen","age":"36","job":"IT engineer","hobby":"basketball","hometown":"Liaoning Province"}
print(dic01)
dic01.update(sex="",xueli="benke")
print(dic01)
View Code

代码运行结果: 

{'hometown': 'Liaoning Province', 'name': 'Li chen', 'hobby': 'basketball', 'job': 'IT engineer', 'age': '36'}
{'xueli': 'benke', 'sex': '男', 'hometown': 'Liaoning Province', 'name': 'Li chen', 'hobby': 'basketball', 'job': 'IT engineer', 'age': '36'}

5.字典:字典查询

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#字典:根据KEY查找对应的values
dic01={"name":"Li chen","age":"36","job":"IT engineer","hobby":"basketball","hometown":"Liaoning Province"}
r=dic01.get("age")
print(r)
v=dic01.get("hometown")
print(v)
View Code

代码运行结果:

36
Liaoning Province

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#字典:根据KEY查找对应的values
dic01={"name":"Li chen","age":"36","job":"IT engineer","hobby":"basketball","hometown":"Liaoning Province"}
r=dic01["job"]
print(r)
View Code

代码运行结果:

IT engineer

 

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#字典:查询所有的KEY都是哪些
dic01={"name":"Li chen","age":"36","job":"IT engineer","hobby":"basketball","hometown":"Liaoning Province"}
r=dic01.keys()
print(r)
View Code

代码运行结果:

dict_keys(['age', 'job', 'hobby', 'hometown', 'name'])

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#字典:查询所有的KEY的values值都有哪些
dic01={"name":"Li chen","age":"36","job":"IT engineer","hobby":"basketball","hometown":"Liaoning Province"}
r=dic01.values()
print(r)
View Code

代码运行结果:

dict_values(['Liaoning Province', 'basketball', '36', 'IT engineer', 'Li chen'])

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#字典:查询所有的KEY以及KEY所对应的values值都有哪些
dic01={"name":"Li chen","age":"36","job":"IT engineer","hobby":"basketball","hometown":"Liaoning Province"}
r=dic01.items()
print(r)
View Code

代码运行结果:

dict_items([('hometown', 'Liaoning Province'), ('hobby', 'basketball'), ('name', 'Li chen'), ('job', 'IT engineer'), ('age', '36')])

6.字典:字典的嵌套

字典的嵌套是非常重要的,这个必须要建立在熟练使用字典的增删改查的基础上,字典的嵌套才是咱们在工作中经常会遇到的字典!

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#字典:字典的嵌套,非常重要!
#需求如下:
# 1. 获取王峰的名字。
#
# 2.获取这个字典:{'name':'小张','age':48}。
#
# 3. 获取王峰妻子的名字。
#
# 4. 获取王峰的第三个孩子名字。

dic01 = {
    'name':'王峰',
    'age':50,
    'wife':[{'name':'小张','age':48}],
    'children':{'girl_first':'小橘子','girl_second':'二橘子','girl_three':'三小张'}
}
#获取王峰的名字
r=dic01.get("name")
print(r)
#获取这个字典:{'name':'小张','age':48}
s=dic01['wife'][0]
print(s)
#获取王峰妻子的名字
v=dic01['wife'][0]["name"]
print(v)
#获取王峰的第三个孩子名字
chdren=dic01["children"]["girl_three"]
print(chdren)
View Code

代码运行结果:

王峰
{'name': '小张', 'age': 48}
小张
三小张

posted on 2021-12-23 14:22  永远的大空翼  阅读(48)  评论(0编辑  收藏  举报