字典
2020-04-23 本文已影响0人
qjh_py
目录
- 字典的基本介绍
- 创建方法
- 访问方法
4.其他内建方法
1. 字典的基本介绍
- 映射类型(mapping)--{key:value}
- 作用:和列表类似,都是用来存储对象的容器
- 列表存储数据的性能好,但是查询数据的性能差,字典正好与之相反
- 也称之为键值对(key-value)结构
- 每个字典中都可以有多个键值对,而每一个键值对我们称其为一项(item)
- 字典的值可以是任意对象,字典的键可以是任意的不可变对象(int str bool tuple....)
- 字典的键是不能重复的,如果出现重复的后面的会替换前面的
2. 创建方法
a = dict(one=1, two=2, three=3)
b = {'one': 1,'two': 2,'three': 3}
c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
d = dict([('two', 2),('one', 1), ('three', 3)])
e = dict({'three': 3, 'one': 1, 'two': 2})
print('\n',a,'\n',b,'\n',c,'\n',d,'\n',e)
# {'one': 1, 'two': 2, 'three': 3}
# {'one': 1, 'two': 2, 'three': 3}
# {'one': 1, 'two': 2, 'three': 3}
# {'two': 2, 'one': 1, 'three': 3}
# {'three': 3, 'one': 1, 'two': 2}
f = dict.fromfeys(s[,v])
#创建并返回新字典。s可用一个元组表示多个键,v是一个值。
3. 访问方法


4.其他内建方法

- 注意字典也是可迭代对象,所以列表的一些方法对字典也适用。