zoukankan      html  css  js  c++  java
  • 4、字典使用

    类似汉语词典

    根据一个词,得到它里面具体的释义。由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,'	',v[0],'	|'
      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 '----------------------------------------
      '
      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
      :



  • 相关阅读:
    C/C++操作MySQL数据库——增、删、改、查
    Mysql常用命令行大全——转载
    .NET Framework、C#、ASP.NET之间的关系
    委托和事件
    ASP.NET 页生命周期概述
    在SqlServer下增加MySql的链接服务器
    Head First 设计模式----DecoratorPattern
    jquery easyui----tree
    Head First 设计模式----ObserverPattern
    Jquery easyui----combotree
  • 原文地址:https://www.cnblogs.com/kissdb/p/4009571.html
Copyright © 2011-2022 走看看