标签:style blog http color 使用 for sp div 2014
类似汉语词典
>>> contact={‘lk‘:‘18610314061‘,‘tom‘:‘10086‘} -- 定义一个字典 >>> contact -- 打印 {‘lk‘: ‘18610314061‘, ‘tom‘: ‘10086‘} >>> contact[‘lk‘] -- 打印一个key的value ‘18610314061‘ >>> print contact[‘lk‘] 18610314061 >>> print contact[‘lk‘][0:3] -- 将value按字符切片 186 >>> contact.keys() -- 打印所有key [‘lk‘, ‘tom‘] >>> contact.values() -- 打印所有value [‘18610314061‘, ‘10086‘] >>> contact.items() -- 打印所有组(key和value) [(‘lk‘, ‘18610314061‘), (‘tom‘, ‘10086‘)] >>> contact[‘jack‘]=‘10010‘ -- 添加一组 >>> contact {‘lk‘: ‘18610314061‘, ‘jack‘: ‘10010‘, ‘tom‘: ‘10086‘} >>> contact.popitem() -- 删除掉第一组 (‘lk‘, ‘18610314061‘) >>> contact {‘jack‘: ‘10010‘, ‘tom‘: ‘10086‘} >>> contact.pop(‘tom‘) -- 根据key删除某一个组 ‘10086‘ >>> contact {‘jack‘: ‘10010‘} >>> contact[‘jack‘]=‘11111‘ -- 修改某个key的value >>> contact {‘jack‘: ‘11111‘} >>> contact.has_key(‘lk‘) -- 判断是否有某个key False >>> contact.has_key(‘jack‘) True >>> contact={‘lk‘:‘18610314061‘,‘tom‘:‘10086‘} >>> for i in contact: -- 用循环输出所有key ... print i ... lk jack tom >>> for key,value in contact.items(): -- 用循环输出所有key和value ... print key,value ... lk 186 jack 11111 tom 10086 >>> contact={ --value中使用list列表 ... ‘lk‘: [‘18610314061‘,‘good‘], ... ‘tom‘:[‘10086‘,‘bad‘] ... } >>> contact[‘lk‘][1] -- 输出value列表中的一项值 ‘good‘
1 #!/usr/bin/python 2 emp_info={ 3 ‘01‘:[‘lk1‘,‘lk@161.com‘,‘it‘,‘18610314061‘], 4 ‘02‘:[‘lk2‘,‘lk@162.com‘,‘it‘,‘18610314062‘], 5 ‘03‘:[‘lk3‘,‘lk@163.com‘,‘it‘,‘18610314063‘], 6 ‘04‘:[‘lk4‘,‘lk@164.com‘,‘it‘,‘18610314064‘] 7 } 8 whileTrue: 9 print ‘ ----------------‘ 10 print ‘| ID NAME |‘ 11 print ‘ ----------------‘ 12 for k,v in emp_info.items(): 13 print ‘|‘,k,‘\t‘,v[0],‘\t|‘ 14 print ‘ ----------------‘ 15 id=raw_input(‘‘‘ 16 ‘[id]‘ to print the emp info 17 ‘add‘ to add a new emp 18 ‘exit‘ to exit the process 19 :‘‘‘) 20 ################## print the emp info ################### 21 if emp_info.has_key(id): 22 print ‘----------------------------------------‘ 23 print ‘|‘,id, 24 for i in emp_info[id]: 25 print i, 26 print ‘|‘ 27 print ‘----------------------------------------\n‘ 28 elif id!=‘add‘ and id!=‘exit‘: 29 print ‘We dont have this emp!‘ 30 ################### add a new emp ####################### 31 if id==‘add‘: 32 new_id=raw_input(‘Please input id:‘) 33 while emp_info.has_key(new_id): 34 new_id=raw_input(‘ID is the same,Please input other id:‘) 35 new_name=raw_input(‘Please input name:‘) 36 new_email=raw_input(‘Please input email:‘) 37 new_job=raw_input(‘Please input job:‘) 38 new_tel=raw_input(‘Please input tel:‘) 39 emp_info[new_id]=[new_name,new_email,new_job,new_tel] 40 ################### exit ################## 41 if id==‘exit‘: 42 break
[root@likun python_scripts]# python 7emp.py ---------------- | ID NAME | ---------------- |02 lk2 | |03 lk3 | |01 lk1 | |04 lk4 | ---------------- ‘[id]‘ to print the emp info ‘add‘ to add a new emp ‘exit‘ to exit the process :
标签:style blog http color 使用 for sp div 2014
原文地址:http://www.cnblogs.com/kissdb/p/4009571.html