>[info] 程序中,大多数的参数都以字典的形式存储。
[TOC]
<br>
### 字典的定义
字典dictionary是一种以key-value键值对的形式存储数据,key是不可变的,可以是字符串,数字或元组,而value可以是任意一种数据类型。
#### 字典的赋值
```python
person={'name':'Milton','age':18,'hobby':['basketball','football']}
```
#### 字典的读取
```cmd
>>> person={'name':'Milton','age':18,'hobby':['basketball','football']}
>>> person['name']
'Milton'
```
#### 字典值的修改
```cmd
>>> person={'name':'Milton','age':18,'hobby':['basketball','football']}
>>> person['name']='Cherish'
>>> person
{'name': 'Cherish', 'age': 18, 'hobby': ['basketball', 'football']}
```
### 字典常用方法
#### clear():清除字典中所有元素
```cmd
>>> person={'name':'Milton','age':18,'hobby':['basketball','football']}
>>> person
{'name': 'Milton', 'age': 18, 'hobby': ['basketball', 'football']}
>>> person.clear()
>>> person
{}
```
#### copy():浅拷贝
```cmd
>>> person={'name':'Milton','age':18,'hobby':['basketball','football']}
>>> person2=person.copy()
>>> person2
{'name': 'Milton', 'age': 18, 'hobby': ['basketball', 'football']}
>>> person['name']='Cherish'
>>> person
{'name': 'Cherish', 'age': 18, 'hobby': ['basketball', 'football']}
>>> person2
{'name': 'Milton', 'age': 18, 'hobby': ['basketball', 'football']}
>>>
```
注意区分copy()与`=`赋值的区别,慎用`=`
```cmd
>>> person={'name':'Milton','age':18,'hobby':['basketball','football']}
>>> person2=person
>>> person['name']='Cherish'
>>> person
{'name': 'Cherish', 'age': 18, 'hobby': ['basketball', 'football']}
>>> person2
{'name': 'Cherish', 'age': 18, 'hobby': ['basketball', 'football']}
```
#### dict.fromkeys(seq[, value]):以一个序列中的值作为key,创建字典
```cmd
>>> person=['Milton','Cherish']
>>> detail=dict.fromkeys(person,{'sex':'M'})
>>> detail
{'Milton': {'sex': 'M'}, 'Cherish': {'sex': 'M'}}
```
#### get(k, d=None):根据key,获取元素值
```cmd
>>> person={'name':'Milton','age':18,'hobby':['basketball','football']}
>>> person.get('name')
'Milton'
>>> person.get('score',99)
99
```
#### items():以列表返回可遍历的(键, 值) 元组数组
```cmd
>>> person={'name':'Milton','age':18,'hobby':['basketball','football']}
>>> person.items()
dict_items([('name', 'Milton'), ('age', 18), ('hobby', ['basketball', 'football'])])
>>>
```
#### keys():以列表返回所有的key
```cmd
>>> person={'name':'Milton','age':18,'hobby':['basketball','football']}
>>> person.keys()
dict_keys(['name', 'age', 'hobby'])
>>>
```
#### values():以列表返回字典中的所有值
```cmd
>>> person={'name':'Milton','age':18,'hobby':['basketball','football']}
>>> person.values()
dict_values(['Milton', 18, ['basketball', 'football']])
```
#### pop(k, d=None):删除key所对应的值,返回被删除的值。
key值必须给出。 否则,返回default值。
```cmd
>>> person={'name':'Milton','age':18,'hobby':['basketball','football']}
>>> person.pop('age')
18
>>> person
{'name': 'Milton', 'hobby': ['basketball', 'football']}
>>>
```
#### popitem():随机返回并删除字典中的一对键和值。
```cmd
>>> person={'name':'Milton','age':18,'hobby':['basketball','football']}
>>> person.popitem()
('hobby', ['basketball', 'football'])
>>> person
{'name': 'Milton', 'age': 18}
```
#### setdefault(self, k, d=None):设置默认值
如果对应的key不存在,则修改字典,并返回设置的默认值
如果对应的key存在,则不修改字典,直接返回原来的值
```cmd
>>> person={'name':'Milton','age':18,'hobby':['basketball','football']}
>>> person.setdefault('name','Cherish')
'Milton'
>>> person
{'name': 'Milton', 'age': 18, 'hobby': ['basketball', 'football']}
>>> person.setdefault('sex','M')
'M'
>>> person
{'name': 'Milton', 'age': 18, 'hobby': ['basketball', 'football'], 'sex': 'M'}
```
#### update(self, E=None, **F):用一个字典更新当前字典
```cmd
>>> person={'name':'Milton','age':18,'hobby':['basketball','football']}
>>> person.update({'name':'Cherish','sex':'female'})
>>> person
{'name': 'Cherish', 'age': 18, 'hobby': ['basketball', 'football'], 'sex': 'female'}
```
<hr style="margin-top:100px">
:-: ![](https://box.kancloud.cn/2ff0bc02ec938fef8b6dd7b7f16ee11d_258x258.jpg)
***微信扫一扫,关注“python测试开发圈”,了解更多测试教程!***
- 前言
- chapter01_开发环境
- chapter02_字符串的使用
- chapter03_列表的使用
- chapter04_字典的使用
- chapter05_数字的使用
- chapter06_元组的使用
- chapter07_集合的使用
- chapter08_输入输出
- chapter09_控制流程
- chapter10_实例练习_登录1
- chapter11_python函数入门
- chapter12_python中的类
- chapter13_轻松玩转python中的模块管理
- chapter14_掌握学习新模块的技巧
- chapter15_通过os模块与操作系统交互
- chapter16_子进程相关模块(subprocess)
- chapter17_时间相关模块(time & datetime)
- chapter18_序列化模块(json)
- chapter19_加密模块(hashlib)
- chapter20_文件的读与写
- chapter21_阶段考核2_登录
- chapter22_小小算法挑战(排序&二分法)
- chapter23_用多线程来搞事!
- chapter24_HTTP接口请求(requests)
- chapter25_接口测试框架(pytest)
- chapter26_阶段考核3_HTTP接口测试
- chapter27_HTML解析(pyquery)
- chapter28_阶段考核4_爬虫下载网易汽车
- chapter29_python中的那些编码坑
- chapter30_MySQL数据库操作
- chapter31 高级特性_迭代器与生成器
- chapter32 高级特性_装饰器
- chapter33 高级特性_列表处理