python:dictionary
作者:
愁容_骑士 | 来源:发表于
2017-02-07 02:30 被阅读0次
概述
常用操作:
- 样式:
grades = {'Ana':'B', 'John':'A+', 'Denise':'A', 'Katy':'A'}
- 赋值:
grades['John'] = 'A'
- 判断 某元素是否在dict里:
in: 'John' in grades
out: True
- 删除
del(grades['Ana']
- 查找key:
grades.keys()
returns ['Denise','Katy','John','Ana']
- 查找value:
grades.values()
returns ['A', 'A', 'A+', 'B']
values
- any type (immutable and mutable)
- can be duplicates 可以重复
- dictionary values can be lists, even other dictionaries!
keys
- must be unique
- immutable type (int, float, string, tuple,bool)
- actually need an object that is hashable, but think of as immutable as all immutable types are hashable
- careful with float type as a key
else:
- dict无顺序
d = {4:{1:0}, (1,3):"twelve", 'const':[3.14,2.7,8.44]}
本文标题:python:dictionary
本文链接:https://www.haomeiwen.com/subject/lhqpittx.html
网友评论