Python 字典
来源:互联网 发布:物理层和mac层 编辑:程序博客网 时间:2024/06/10 01:42
字典是Python语言中唯一的映射类型。
映射类型对象里哈希值(键,key)和指向的对象(值,value)是一对多的的关系,通常被认为是可变的哈希表。
字典对象是可变的,它是一个容器类型,能存储任意个数的Python对象,其中也可包括其他容器类型。
字典类型与序列类型的区别:
1. 存取和访问数据的方式不同。
2. 序列类型只用数字类型的键(从序列的开始按数值顺序索引)。
3. 映射类型可以用其他对象类型作键(如:数字、字符串、元组,一般用字符串作键),和序列类型的键不同,映射类型的键接或间接地和存储数据值相关联。
4. 映射类型中的数据是无序排列的。这和序列类型是不一样的,序列类型是以数值序排列的。
5. 映射类型用键直接“映射”到值。
字典是Python中最强大的数据类型之一。
# 直接声明adict={1:'harry',2:'tom'}# 使用 dict()函数cdict=dict((['x',1],['y',2])) #使用[]zdict= dict(name='allen',age='40') #使用 =ydict=dict({'three': 3, 'one': 1, 'two': 2,'four':4})cdict = dict(zip(['one', 'two', 'three','four'], [1, 2, 3,4])) # 使用zip函数
遍历字典
#遍历字典d = {'a':1, 'b':0, 'c':1, 'd':0,'e':8}for k in d.keys(): print(k)
另一种遍历方法
d = {'a':1, 'b':0, 'c':1, 'd':0,'e':9}for k in d: print(k)
同时获取key 和value
dict = {'a':1, 'b':0, 'c':1, 'd':0,'e':9}for (d,x) in dict.items(): print ("key:"+d+",value:"+str(x))
dict = {'a':1, 'b':0, 'c':1, 'd':0,'e':9}for d,x in dict.items(): print ("key:"+d+",value:"+str(x))
0 0
- Python 字典
- python 字典
- Python:字典
- Python 字典
- python字典
- Python 字典
- python 字典
- python 字典
- Python 字典
- Python字典
- python 字典
- python字典
- python 字典
- python 字典
- python--字典
- python 字典
- python字典
- Python---字典
- CSS3高级特性之二维动画
- 【Linux 内核网络协议栈源码剖析】connect 函数剖析(二)
- 线性表的学习之路
- 如何阅读Java源码 阅读java的真实体会
- 微信 jssdk获取经纬度转换成百度的经纬度
- Python 字典
- 字符输出流(写)常用方法
- DFS hdu2266(How Many Equations Can You Find)
- 杭电 2058 the sum problem
- android 实现剪贴板的粘贴复制
- poj_1573 Robot Motion(简单模拟)
- Linux signal, wait, waitpid 函数
- 跨平台APP----对Cordova,APPCan,DCloud,APICloud四大平台的分析
- 远程访问路由器下电脑