Python 字典(Dictionary)
来源:互联网 发布:python serial 模块 编辑:程序博客网 时间:2024/06/13 08:11
一、定义
字典是另一种可变容器模型,且可存储任意类型对象。
二、创建
字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中 ,格式如下所示:
dict={'name':'刘备','age':1000,'birthday':'公元前'}
键必须不可变,所以可以用数字,字符串或元组充当
三、访问
dict={'name':'刘备','age':'1000','birthday':'公元前'}print(dict['name'])#刘备
四、修改
dict={'name':'刘备','age':'1000','birthday':'公元前'}dict['name']='刘备2'print(dict)#{'name': '刘备2', 'age': '1000', 'birthday': '公元前'}
五、删除
dict={'name':'刘备','age':'1000','birthday':'公元前'}del dict['name']print(dict)#{'age': '1000', 'birthday': '公元前'}
六、字典内置函数&方法
1.len(dict) 计算字典元素个数,即键的总数。
dict={'name':'刘备','age':'1000','birthday':'公元前'}print(len(dict))#3
2.copy()方法于=方法
dict={'name':'刘备','age':'1000','birthday':'公元前'}dict2=dict.copy()print(dict2)#{'name': '刘备', 'age': '1000', 'birthday': '公元前'}copy()与赋值比较dict={'name':'刘备','age':'1000','birthday':'公元前'}dict2=dict.copy()print('dict2>',dict2)dict3=dictprint('dict3>',dict3)dict['name']='liub'print('dict2',dict2)#{'name': '刘备', 'age': '1000', 'birthday': '公元前'}print('dict3',dict3)print('dict',dict)打印:dict2> {'name': '刘备', 'age': '1000', 'birthday': '公元前'}
dict3> {'name': '刘备', 'age': '1000', 'birthday': '公元前'}
dict2 {'name': '刘备', 'age': '1000', 'birthday': '公元前'}
dict3 {'name': 'liub', 'age': '1000', 'birthday': '公元前'}
dict {'name': 'liub', 'age': '1000', 'birthday': '公元前'}
上例中:dict3是引用的dict同一个地址,所以会dict的改变而改变,dict2可以理解成dict的复制品,不会随着dict的改变而改变。如果还不能理解,请查看本文的深浅拷贝
阅读全文
0 0
- Python dictionary 字典
- python Dictionary字典使用
- Python字典 (dictionary)
- Python 字典(Dictionary)
- Python 字典(Dictionary)
- Python字典 (dictionary)
- Python 字典(Dictionary)
- Python字典dictionary
- Python字典(Dictionary)
- Python菜鸟--字典(Dictionary)
- 1006- Python 字典(Dictionary)
- Python 字典(Dictionary)
- Python字典 (dictionary)
- Python 字典(Dictionary)数据处理
- Python 字典(Dictionary)
- Python 字典(Dictionary)
- python字典(Dictionary)-整理
- Python 字典(Dictionary)
- 11月7日笔记
- mybatis中mapUnderscoreToCamelCase自动驼峰命名转换
- 413. Arithmetic Slices
- HashMap加入数据后,会自动根据首字母排序
- react进阶之异常处理机制-error Boundaries
- Python 字典(Dictionary)
- DELL 服务器启动出现NO boot device availabel解决方法
- 大数据浪潮下 你也可以转型大数据工程师!
- win 10 opencv dubeg 应用程序无法正常启动0xc00007b
- 数据库自动化运维平台--自助建表建库
- Glide加载圆形image第一次显示占位图的解决方法
- 虚拟机ubuntu16.04 安装eclipse
- 几句话让你明白约束的作用
- Linux下压缩和解压缩命令