Python2.7之字典

字典(dic)//我觉得是列表的另一种用法,只是用列表比较麻烦,可以用字典来简化,元素采用key-value对应方法,删除key时对应的值也会被删除

>>>dic = {"Tom":85,"Jhon":70,"Linda":90}

>>>print dic 

{"Tom":85,"Jhon":70,"Linda":90}

-------------------------------------------------------------------------

查询某个元素是否在字典中时

(1)>>> "Tom" in dic

                   True

           >>>"Jam" in dic

                  False

(2)>>>dic.get("Tom")

           85

     >>>dic.get("Jam") //不会返回值,或者自定义值:dic.get("Jam",-1)不存在返回-1

-----------------------------------------------------------------------------------------------------------------

>>>dic.pop('Tom')

85

>>>print dic

{"Jhon":70,"Linda":90}

学习自廖雪峰官网:http://www.liaoxuefeng.com/

你可能感兴趣的:(Python2.7之字典)