标签:
>>> d={}
>>> d[‘name‘]={‘firstname‘:‘xian‘,‘lastname‘:‘jiaqi‘} #添加一个值
>>> d
{‘name‘: {‘lastname‘: ‘jiaqi‘, ‘firstname‘: ‘xian‘}}
>>> d[‘aihao‘]=[‘skite‘,‘football‘,‘pingpang‘,]
>>> d
{‘name‘: {‘lastname‘: ‘jiaqi‘, ‘firstname‘: ‘xian‘}, ‘aihao‘: [‘skite‘, ‘football‘, ‘pingpang‘]}
>>> d[‘age‘]=5
>>> d
{‘age‘: 5, ‘name‘: {‘lastname‘: ‘jiaqi‘, ‘firstname‘: ‘xian‘}, ‘aihao‘: [‘skite‘, ‘football‘, ‘pingpang‘]}
>>> d[‘age‘]+=1
>>> d
{‘age‘: 6, ‘name‘: {‘lastname‘: ‘jiaqi‘, ‘firstname‘: ‘xian‘}, ‘aihao‘: [‘skite‘, ‘football‘, ‘pingpang‘]}
>>> d[‘name‘][‘lastname‘]
‘jiaqi‘
>>> d[‘aihao‘][2]
‘pingpang‘
>>> d[‘aihao‘].append(‘play‘) #aihao的内容为list则可钓鱼list的方法
>>> d[‘aihao‘]
[‘skite‘, ‘football‘, ‘pingpang‘, ‘play‘]
>>> a={‘name‘:{‘firstname‘:‘xian‘,‘lastname‘:‘jiaqi‘},‘aihao‘:[‘skite‘,‘football‘,‘pingpang‘],‘age‘:24}
>>> a
{‘age‘: 24, ‘name‘: {‘lastname‘: ‘jiaqi‘, ‘firstname‘: ‘xian‘}, ‘aihao‘: [‘skite‘, ‘football‘, ‘pingpang‘]}
>>>
#对字典排序
a={‘c‘: 3, ‘b‘: 2, ‘a‘: 1}
>>> for key in sorted(a):
print(key,a[key])
a 1
b 2
c 3
标签:
原文地址:http://www.cnblogs.com/xianhaiyan/p/4764954.html