多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
>[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测试开发圈”,了解更多测试教程!***