在Python中,字典{dict}是比较常用的一个数据类型,使用键-值(key-value)存储
与列表[list]相比,字典具有极快的查找和插入速度,不会随着key-value的增加而变慢,但是相应的,字典将占用比列表更多的内存空间
与列表和元组相比较,字典比较典型的特点有:
1.字典是无序的
2.字典中的key必须是唯一的key
3.key必须是不可变的对象:字符串、整数等都是不可变的,可以作为key;而list是可变的,不能作为key
字典的操作相对来说比较简单,而且因为字典是无序的,所以无法通过索引下标输出特定的值

字典的遍历与其他数据类型不同,因为key-value实际上存储了两个值,所以可以用不同方法输出自己想要的值
1,遍历key

运行的结果为

2,遍历value

运行的结果为:

3,同时遍历key-value

运行的结果为:

4,dict的enumerate用法

运行的结果为:

网友评论