码迷,mamicode.com
首页 > 其他好文 > 详细

关于字典的学习

时间:2015-08-28 00:28:07      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:

>>> 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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!