修炼Python基础篇-字典(Dictionary)学习

字典是另一种可变容器模型,且可存储任意类型对象,字典的每个键值(key=>value)对用冒号(:)分割,每个键值对之间用逗号(,)分割,整个字典包括在花括号({})中;

  字典特性:

     1.不允许同一个键出现两次,创建时如果同一个键被赋值两次,最后一个键值生效;

     2.键值必须不可变,可以用数字、字符串或元组充当,因列表是可变的,所以不可以用列表做为键值;

  1. 访问字典中的元素;

    1. dict ={'shanghai':'pudong','sichuan':'chengdu'}
    2. print(dict)
    3. {'sichuan':'chengdu','shanghai':'pudong'}
    4. print(dict['sichuan'])
    5. chengdu

     

  2. 修改字典中的值;

    1. dict ={'shanghai':'pudong','sichuan':'chengdu'}
    2. dict['sichuan']='bazhong'
    3. print(dict['sichuan'])
    4. bazhong

     

  3. 删除字典中的元素;

    1. dict ={'shanghai':'pudong','sichuan':'chengdu'}
    2. del dict['shanghai']#删除键是shanghai的值;
    3. print(dict)
    4. {'sichuan':'chengdu'}
    5. dict.clear()#情况字典所有内容;
    6. print(dict)
    7. {}

     

  4. len:计算字典元素个数;

    1. dict ={'shanghai':'pudong','sichuan':'chengdu'}
    2. print(len(dict))
    3. 2

     

  5. str:输出字典可打印成字符串表示;

    1. dict ={'shanghai':'pudong','sichuan':'chengdu'}
    2. print(str(dict))
    3. {'shanghai':'pudong','sichuan':'chengdu'}
    4. print(type(str(dict)))
    5. <class'str'>

     

  6. type:返回输入的变量类型;

    1. dict ={'shanghai':'pudong','sichuan':'chengdu'}
    2. print(type(dict))
    3. <class'dict'>

     

  7. clear:删除字典内所有元素;

    1. dict ={'shanghai':'pudong','sichuan':'chengdu'}
    2. dict.clear()
    3. print(dict)
    4. {}

     

  8. copy:浅复制一个字典;

    1. dict ={'shanghai':'pudong','sichuan':'chengdu'}
    2. dict1 = dict.copy()
    3. print(dict1)
    4. {'shanghai':'pudong','sichuan':'chengdu'}

     

  9. fromkey:创建一个新的字典,以序列seq中的元素作为字典的键,value为字典所有键对应的初始值;

    1. seq ='ShangHai','Beijing','SiChuan'
    2. dict=dict.fromkeys(seq,100)
    3. print(dict)
    4. {'Beijing':100,'ShangHai':100,'SiChuan':100}

     

  10. get:返回指定键的值,如果值不在字典内就返回None;

    1. dict ={'shanghai':'pudong','sichuan':'chengdu'}
    2. print(dict.get('shanghai'))
    3. pudong
    4. print(dict.get('tianjing'))
    5. None

     

  11. in:如果键在字典内存在就返回True,否则返回False;

    1. dict ={'shanghai':'pudong','sichuan':'chengdu'}
    2. print('shanghai'in dict)
    3. True
    4. print('jiangsu'in dict)
    5. False

     

  12. items:以列表方式返回可遍历的(键、值)元组数据;

    1. dict ={'shanghai':'pudong','sichuan':'chengdu'}
    2. print(dict.items())
    3. dict_items([('shanghai','pudong'),('sichuan','chengdu')])

     

  13. pop:删除指定的键值;

    1. dict ={'shanghai':'pudong','sichuan':'chengdu'}
    2. dict.pop('shanghai')
    3. 'pudong'
    4. print(dict)
    5. {'sichuan':'chengdu'}

     

  14. popitems:随机返回并删除字典中的一对键和值(项)。为什么是随机删除呢?因为字典是无序的,没有所谓的“最后一项”或是其它顺序。在工作时如果遇到需要逐一删除项的工作,用popitem()方法效率很高。

    1. dict ={'shanghai':'pudong','sichuan':'chengdu'}
    2. dict.popitem()
    3. ('shanghai','pudong')
    4. print(dict)
    5. {'sichuan':'chengdu'}

     

  15. setdefault:如果键不已经存在于字典中,将会添加键并将值设为默认值;

    1. dict ={'shanghai':'pudong','sichuan':'chengdu'}
    2. print("Value1:%s"% dict.setdefault('sichuan'))
    3. Value1:chengdu
    4. print("Value1:%s"% dict.setdefault('beijing'))
    5. Value1:None
    6. print(dict)
    7. {'beijing': None, 'shanghai': 'pudong', 'sichuan': 'chengdu'}

     

  16. update:把字典dict2的键、值对更新到dict里面;

    1. dict ={'shanghai':'pudong','sichuan':'chengdu'}
    2. dict1 ={'beijing':'chaoyang','guangdong':'shengzhen'}
    3. dict.update(dict1)
    4. print(dict)
    5. {'guangdong':'shengzhen','sichuan':'chengdu','shanghai':'pudong','beijing':'chaoyang'}

     

  17. values:以列表的方式返回字典里的值;

    1. dict1 ={'beijing':'chaoyang','guangdong':'shengzhen'}
    2. print(dict.values())
    3. dict_values(['shengzhen','chengdu','pudong','chaoyang'])

     

posted @ 2017-01-17 09:52  Saviorsyang  阅读(293)  评论(0编辑  收藏  举报