Kiss my DB

你所浪费的今天,正是昨日我熬到深夜所期待的明天...珍惜当下,无论生活工作

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

类似汉语词典

根据一个词,得到它里面具体的释义。由key和value组成
key必须唯一
value可以用list组成多值
 
 
>>> 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.  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

     

  1. [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
    :

     



posted on 2014-10-07 17:33  小kiss  阅读(307)  评论(0编辑  收藏  举报