Python容器:1_字典类型及其基础操作


字典是python中的常用的数据类型,在编写自动化脚本时,常常需要对字典进行基本操作。

1. 字典的特点

  • 定界符 : {}
  • 是否支持下标 :是(使用”键“作为下标)
  • 元素形式 : 键 : 值
  • 元素是否可重复 :'键不允许,'值’允许
  • 元素查找速度 : 非常快
  • 增删速度 :快

2. 字典创建

2.1 使用 {} 创建字典

# 使用 = 给字典复制变量
dict_a = {}  # 重点掌握
# 详细案例
dict_b = {'a': 111, 'b': 2222, 'c': 111}  # 重点掌握
dict_c = {'a': 111, 'b': 2222, 333: 'hello'}
dict_d = {'a': 111, 'b': 2222, 333: 'hello', 'd': dict_c}
dict_d = {'a': {}, 'b': 2222, 'c': {}, 'd': {}} 
print(dict_a)
print(dict_b)
print(dict_c)
print(dict_d)

2.2 使用内置函数 dict()创建字典

# 使用 dict() 创建字典
d_a = dict()
d_b = dict(a=111)
print(d_a)
print(d_b)
# 案例:
student_dict = {'no1': 'Tom', 'no2': 'Ammy', 'no3': 'Lucy', 'no4': 'Bob'}
print(student_dict)

3. 字典访问

3.1 通过key访问value


student_dict = {'no1': 'Tom', 'no2': 'Ammy', 'no3': 'Lucy', 'no4': 'Bob'}
print(student_dict['no1'])

3.2 通过get()访问


student_dict = {'no1': 'Tom', 'no2': 'Ammy', 'no3': 'Lucy', 'no4': 'Bob'}
print(student_dict.get('no1'))

4. 字典其他操作

4.1 访问所有字典元素的键key


student_dict = {'no1': 'Tom', 'no2': 'Ammy', 'no3': 'Lucy', 'no4': 'Bob'}
print(student_dict.keys())

4.2 访问所有字典元素的值value


student_dict = {'no1': 'Tom', 'no2': 'Ammy', 'no3': 'Lucy', 'no4': 'Bob'}
print(student_dict.values())

4.3 访问所有的字典元素


student_dict = {'no1': 'Tom', 'no2': 'Ammy', 'no3': 'Lucy', 'no4': 'Bob'}
print(student_dict.items())

# 按 no1---------Tom 的格式打印所有的字典元素
for key, value in student_dict.items():
    print(f'{key}-----------{value}')

5. 字典元素的添加和修改

5.1 通过key增加value


student_dict = {'no1': 'Tom', 'no2': 'Ammy', 'no3': 'Lucy', 'no4': 'Bob'}
student_dict['no5'] = 'Harriet'
print(student_dict)

5.2 通过update()增加value


student_dict = {'no1': 'Tom', 'no2': 'Ammy', 'no3': 'Lucy', 'no4': 'Bob'}
student_dict.update(no5='Chars') # 注意,此方法的字典键不加单引号
print(student_dict)

5.3 字典修改

语法和新增一样,如果字典中没有对应的元素,就是新增;如果有对应的元素,就是修改

6. 字典元素的删除

6.1 使用 pop() 删除字典元素

  • 根据key值删除对应的字典元素

student_dict = {'no1': 'Tom', 'no2': 'Ammy', 'no3': 'Lucy', 'no4': 'Bob'}
student_dict.pop('no1') # 删除一个字典元素 加引号
print(student_dict)

6.2 使用 popitem() 删除字典元素

  • python3.6之前默认随机删除一个字典元素,python3.6之后默认删除并返回最后一个字典元素

student_dict = {'no1': 'Tom', 'no2': 'Ammy', 'no3': 'Lucy', 'no4': 'Bob'}
student_dict.popitem() # 默认从后往前删除
print(student_dict)
# 验证如下
student_dict.popitem()
print(student_dict)
student_dict.popitem()
print(student_dict)

6.3 使用 clear() 删除字典元素

  • 删除字典中的所有元素,但是保留了字典

student_dict = {'no1': 'Tom', 'no2': 'Ammy', 'no3': 'Lucy', 'no4': 'Bob'}
student_dict.clear() # 删除字典中的所有元素
print(student_dict)

6.4 使用 del 删除字典

  • del删除了整个字典数据

student_dict = {'no1': 'Tom', 'no2': 'Ammy', 'no3': 'Lucy', 'no4': 'Bob'}
del student_dict # del删除了整个字典数据,因此打印会报错
# print(student_dict)
  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值