特点
- key-value集合,存储的key-value序对是没有顺序
- dict的第一个特点是查找速度快,无论dict有10个元素还是10万个元素,查找速度都一样。而list的查找速度随着元素增加而逐渐下降
- dict的查找速度快不是没有代价的,dict的缺点是占用内存大,还会浪费很多内容,list正好相反,占用内存小,但是查找速度慢
- dict是按 key 查找,所以,在一个dict中,key不能重复
- 特点就是
Python的基本类型如字符串、整数、浮点数都是不可变的都可以作位但是list是可变的就不能作为key.
常用API
d = {
'1': 'a',
'2': 'b',
'3': 'c'
}
print(len(d))
d['1']
d.get('1')
for key in d:
print(key);
for key, in d.iterkeys():
print(key)
for key, in d.itervalues():
print(key)
for key,value in d.items():
print(key,value)
for key,value in d.iteritems():
print(key,value)
d.update({'Pall': 90})
print(d)
d.pop('Pall')
print(d)
d['1']='Z'
print(d)