zx
#####################创建字典######################################
In [11]: dict([(‘name‘,‘bob‘),(‘age‘,23),[‘qq‘,123456]]) Out[11]: {‘age‘: 23, ‘name‘: ‘bob‘, ‘qq‘: 123456} In [12]: dict(((‘name‘,‘bob‘),(‘age‘,23),[‘qq‘,123456])) ##dict函数 Out[12]: {‘age‘: 23, ‘name‘: ‘bob‘, ‘qq‘: 123456} In [13]: aDict = {‘name‘:‘bob‘,‘age‘:23,‘qq‘:123456} ##{}方式 In [14]: aDict Out[14]: {‘age‘: 23, ‘name‘: ‘bob‘, ‘qq‘: 123456}
In [20]: {}.fromkeys([‘bob‘,‘herry‘,‘jerry‘],7) ##不同的key,相同的值,‘7’为值value
Out[20]: {‘bob‘: 7, ‘herry‘: 7, ‘jerry‘: 7}
############关于循环字典取值###############
In [22]: for key in aDict: ##默认只取key ....: print key ....: qq age name In [23]: for key in aDict: ##key与value一起取 print ‘%s-->%s‘ % (key,aDict[key]) ....: qq-->123456 age-->23 name-->bob
#########特殊格式输出################In [36]: aDict Out[36]: {‘age‘: 23, ‘name‘: ‘bob‘, ‘qq‘: 123456} In [37]: ‘%(name)s --> %(age)s‘ % aDict Out[37]: ‘bob --> 23‘
##############删除(不常用),弹出与清空#############
#######aDict.pop########## In [39]: aDict Out[39]: {‘age‘: 23, ‘name‘: ‘bob‘, ‘qq‘: 123456} In [41]: aDict.pop(‘age‘) ###因为字典是无序的,所以需要指名pop哪个key Out[41]: 23 In [42]: aDict Out[42]: {‘name‘: ‘bob‘, ‘qq‘: 123456}
In [43]: aDict.popitem() ###popitem()是随机弹的,几乎用不到,括号内不接受参数
Out[43]: (‘qq‘, 123456)
In [46]: uu = aDict.copy() #复制字典
In [47]: uu
Out[47]: {‘name‘: ‘bob‘}
In [48]: uu.clear() ###清空字典
In [49]: uu
Out[49]: {}
##########字典的in 与not in #############
In [63]: aDict Out[63]: {‘age‘: 23, ‘name‘: ‘herry‘} In [64]: ‘age‘ in aDict ###通过key查看是否在字典中,而不是value Out[64]: True
#############字典长度##############
In [63]: aDict Out[63]: {‘age‘: 23, ‘name‘: ‘herry‘} In [66]: len(aDict) Out[66]: 2
###########判断是否可以作为字典的key##########
In [67]: hash(‘naem‘) Out[67]: -4166578487153698939 In [68]: hash(20) Out[68]: 20 In [69]: hash((10,30)) Out[69]: 3713074054223687331 In [70]: hash([10,30]) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-70-2766bfecf01d> in <module>() ----> 1 hash([10,30]) TypeError: unhashable type: ‘list‘ 总结:通过hash函数,如果不报错,就是可以作为字典的key,我们会发现key都是不可变对象
#############字典的拷贝################
In [71]: aDict Out[71]: {‘age‘: 23, ‘name‘: ‘herry‘} In [72]: bDict = aDict.copy() In [73]: bDict Out[73]: {‘age‘: 23, ‘name‘: ‘herry‘} In [74]: aDict Out[74]: {‘age‘: 23, ‘name‘: ‘herry‘} In [75]: id(aDict) Out[75]: 45174528 In [76]: id(bDict) Out[76]: 45199760
###########字典的get 用法(常用)############
In [71]: aDict Out[71]: {‘age‘: 23, ‘name‘: ‘herry‘} In [77]: aDict.get(‘age‘) ###get key为‘age’ 的值23 Out[77]: 23 In [78]: aDict.get(‘qq‘) ###get 不存在的key,返回值None In [79]: print aDict.get(‘qq‘) None In [80]: aDict.get(‘qq‘,‘not found‘) ########自定义,如果get 的key存在,则返回对应的值,不存在则返回我们定义的值,这边是‘not found’ Out[80]: ‘not found‘ In [81]: aDict.get(‘age‘,‘not found‘) Out[81]: 23 In [82]: aDict.get(‘name‘,‘not found‘) Out[82]: ‘herry‘ 备注:
In [83]: help(aDict.get)
Help on built-in function get:
get(...)
D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.
In [90]: aDict.setdefault(‘age‘,33) ####setdefault key,存在,则返回相对应的值,不存在则增加key-value Out[90]: 20 In [91]: aDict Out[91]: {‘age‘: 20, ‘name‘: ‘herry‘} In [92]: aDict.setdefault(‘qq‘,333333) Out[92]: 333333 In [93]: aDict Out[93]: {‘age‘: 20, ‘name‘: ‘herry‘, ‘qq‘: 333333} In [94]: help(aDict.setdefault) Help on built-in function setdefault: setdefault(...) D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D
##########################################################
In [100]: aDict Out[100]: {‘age‘: 20, ‘name‘: ‘herry‘, ‘qq‘: 333333} In [96]: aDict.keys() Out[96]: [‘qq‘, ‘age‘, ‘name‘] In [97]: aDict.values() Out[97]: [333333, 20, ‘herry‘] In [98]: aDict.items() Out[98]: [(‘qq‘, 333333), (‘age‘, 20), (‘name‘, ‘herry‘)]
实战案例:
################################################################