字典

来源:互联网 发布:淘宝发顺丰多少钱一个 编辑:程序博客网 时间:2024/05/16 17:52

                                           字典


字典的用处:

    字典可以把两个相对应的元素用一个来表示;
    在不用字典之前我们要进行两个不同列表的元素对应判断时 需要用索引进行一一对比,才能判断其是否匹配,这样要进行的步骤就增多,给我们加大了工作量 
    例 :
       

In [1]: user =['cat','dog','cow']        In [2]: passwd =['hello','hello','hello']        In [3]: zip(user,passwd)       Out[3]: [('cat', 'hello'), ('dog','hello'), ('cow', 'hello')]


字典的特点:
     字典是无序的,所以字典不可以使用索引来查找;
    


字典的建立:

    字典的建立格式:
        dic ={"key":"value","":"","":""}

示例:
In [4]: dic ={"cat":"hello","dog":"hi","cow":"hey"}In [5]: dicOut[5]: {'cat': 'hello', 'cow': 'hey', 'dog': 'hi'}In [6]: dic["cow"] == hello---------------------------------------------------------------------------NameError                                Traceback (most recent call last)<ipython-input-6-ec0d1c60e7d7> in <module>()----> 1 dic["cow"] == helloNameError: name 'hello' is not definedIn [7]: dic["cow"]Out[7]: 'hey'In [8]: dic["cow"] == "hello"Out[8]: FalseIn [9]: 






字典的用法:

(1)字典元素的添加:

这里我们有两种添加方式:

    1) dic[key] =value               ##直接给字典dic内添加相应的元素
    2)dic.update(dic1)               ##使用第二的字典来刷新本个字典

注: 这里使用dic.update(dic1)命令时 如果两个字典里有相同的key 但其的值不同时会进行相应的更新

示例:
######################################################################
In [9]: dicOut[9]: {'cat': 'hello', 'cow': 'hey', 'dog': 'hi'}In [10]: dic["lili"] = '123'In [11]: dicOut[11]: {'cat': 'hello', 'cow': 'hey', 'dog': 'hi', 'lili': '123'}


######################################################################
In [15]: dic1 = {"koko":'123',"kaka":'456'}In [16]: dicOut[16]: {'cat': 'hello', 'cow': 'hey', 'dog': 'hi', 'lili': '123'}In [17]: dic1Out[17]: {'kaka': '456', 'koko': '123'}In [18]: dic.update(dic1)In [19]: dicOut[19]:{'cat': 'hello', 'cow': 'hey', 'dog': 'hi', 'kaka': '456', 'koko': '123', 'lili': '123'}


######################################################################

(2)字典元素的删除:

这里元素的删除有三种:

    1)dic.pop(key)                根据key值删除字典的元素;
    2)dic.popitem()               随机删除字典元素,返回(key,value)
    3)dic.clear()                 删除字典中的所有元素
字典本身的删除:
    del dic                    删除字典本身

示例:
########################################################################
In [20]: dicOut[20]:{'cat': 'hello', 'cow': 'hey', 'dog': 'hi', 'kaka': '456', 'koko': '123', 'lili': '123'}In [21]: dic.pop("lili")Out[21]: '123'In [22]: dicOut[22]: {'cat': 'hello', 'cow': 'hey', 'dog': 'hi', 'kaka': '456', 'koko':'123'}In [23]: dic.popitem()Out[23]: ('kaka', '456')In [24]: dic1Out[24]: {'kaka': '456', 'koko': '123'}In [25]: dic1.clear()In [26]: dic1Out[26]: {}In [27]: del(dic1)In [28]: dic1---------------------------------------------------------------------------NameError                                Traceback (most recent call last)<ipython-input-28-c95a6e8d4d0a> in <module>()----> 1 dic1NameError: name 'dic1' is not definedIn [29]: 


#############################################################################

(3)字典的其他用法:

    1)dict.get()          如果key存在于字典中,返回对应value值
    2)dic.keys()          返回字典的所有key值
    3)dict.has_keys()     字典中是否存在某个key值

示例:

In [31]: dic.keys()Out[31]: ['cow', 'koko', 'dog', 'cat']In [32]: dic.values()Out[32]: ['hey', '123', 'hi', 'hello']In [33]: dic.get("koko")Out[33]: '123'In [34]: dic.get("hello")In [35]: print dic.get("hello")NoneIn [36]: dic.has_key("cat")Out[36]: TrueIn [38]: for key in dic.keys():   ....:     print "key=%s" % key   ....:    key=cowkey=kokokey=dogkey=catIn [40]: dic.items()Out[40]: [('cow', 'hey'), ('koko', '123'), ('dog', 'hi'), ('cat','hello')]