1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
| d = {'name':'jason','age':20,'gender':'male'}
# 判断元素在不在集合内
‘name' in d
> True
'male' in d
> True
# 索引键
d['name']
> 'jason'
# get(key, default) 函数进行索引。如果键不存在,调用get()函数可以返回一个默认值
d.get('sex','null')
> 'null'
# 增加元素
d['dob'] = '1999-02-12'
> d = {'name':'jason','age':20,'gender':'male','dob':'1999-02-12'}
# 更新键对应的值
d['name'] = 'makes'
> d = {'name':'makes','age':20,'gender':'male'}
# 删除键对应的值
d.pop['name']
> d = {'age':20,'gender':'male'}
# 根据字典键的升序排序
d = {'b': 1, 'a': 2, 'c': 10}
d_sorted_key = sorted(d.items(),key=lambda x:x[0])
[('a',2),('b',1),('c',10)]
# 根据字典值的升序排序
d = {'b': 1, 'a': 2, 'c': 10}
d_sorted_value = sorted(d.items(),key=lambda x:x[1])
[('b',1),('a',2),('c',10)]
|