[关闭]
@nalan90 2017-07-28T21:08:16.000000Z 字数 2881 阅读 519

专题三 dict常用操作

Python高效编程技巧实战


常用函数

创建字典
  1. In [1]: d = {'name':'zhangshuang','age':27,'sex':'male'}
  2. In [2]: d
  3. Out[2]: {'age': 27, 'name': 'zhangshuang', 'sex': 'male'}
  4. In [3]: d = dict((['name','zhangshuang'],['age',27]))
  5. In [4]: d
  6. Out[4]: {'age': 27, 'name': 'zhangshuang'}
  7. In [5]: d = dict.fromkeys(['a','b','c'],100)
  8. In [6]: d
  9. Out[6]: {'a': 100, 'b': 100, 'c': 100}

访问字典里的值
  1. In [7]: d = {'name':'zhangshuang','age':27,'sex':'male'}
  2. In [11]: d.get('name')
  3. Out[11]: 'zhangshuang'
  4. ## 使用get函数,当key不存在时,返回None
  5. In [12]: d.get('abc')
  6. In [13]: d['name']
  7. Out[13]: 'zhangshuang'
  8. ## 直接访问,当key不存在,抛错
  9. In [14]: d['abc']
  10. ---------------------------------------------------------------------------
  11. KeyError Traceback (most recent call last)
  12. <ipython-input-14-e51fd128be60> in <module>()
  13. ----> 1 d['abc']
  14. KeyError: 'abc'
  15. ## has_key可判断dict里是否存在一个某个key
  16. In [15]: if d.has_key('abc'):
  17. ....: print d['abc']
  18. ....: else:
  19. ....: print 'key not exists'
  20. ....:
  21. key not exists

删除字典元素
  1. In [21]: d = {'age': 27, 'name': 'zhangshuang', 'sex': 'male'}
  2. In [22]: d
  3. Out[22]: {'age': 27, 'name': 'zhangshuang', 'sex': 'male'}
  4. In [23]: del d['age']
  5. In [24]: d.pop('sex')
  6. Out[24]: 'male'
  7. In [25]: d
  8. Out[25]: {'name': 'zhangshuang'}
  9. In [26]: d.clear()
  10. In [27]: d
  11. Out[27]: {}
  12. In [29]: d.popitem()
  13. Out[29]: ('age', 27)
  14. In [30]: d
  15. Out[30]: {'name': 'zhangshuang', 'sex': 'male'}
  16. In [31]: d.popitem()
  17. Out[31]: ('name', 'zhangshuang')

其他操作
  1. In [33]: d = {'age': 27, 'name': 'zhangshuang', 'sex': 'male'}
  2. ## 以list形式返回所有key
  3. In [34]: d.keys()
  4. Out[34]: ['age', 'name', 'sex']
  5. ## 以list形式返回所有value
  6. In [35]: d.values()
  7. Out[35]: [27, 'zhangshuang', 'male']
  8. ## 以list返回dict所有key-value
  9. In [36]: d.items()
  10. Out[36]: [('age', 27), ('name', 'zhangshuang'), ('sex', 'male')]
  11. ## dict遍历key value
  12. In [39]: for k,v in d.iteritems():
  13. ....: print k, v
  14. ....:
  15. age 27
  16. name zhangshuang
  17. sex male
  18. ## dict遍历key
  19. In [40]: for k in d.iterkeys():
  20. ....: print k
  21. ....:
  22. age
  23. name
  24. sex
  25. ## dict遍历value
  26. In [41]: for v in d.itervalues():
  27. ....: print v
  28. ....:
  29. 27
  30. zhangshuang
  31. male
  32. In [43]: d
  33. Out[43]: {'age': 27, 'name': 'zhangshuang', 'sex': 'male'}
  34. In [44]: d1 = {'age':30,'school':'MIT'}
  35. ## dict更新操作
  36. In [45]: d.update(d1)
  37. In [46]: d
  38. Out[46]: {'age': 30, 'name': 'zhangshuang', 'school': 'MIT', 'sex': 'male'}
  39. In [47]: dict.setdefault?
  40. Docstring: D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D
  41. Type: method_descriptor
  42. In [48]: d
  43. Out[48]: {'age': 30, 'name': 'zhangshuang', 'school': 'MIT', 'sex': 'male'}
  44. In [49]: d.setdefault('age',34)
  45. Out[49]: 30
  46. In [50]: d
  47. Out[50]: {'age': 30, 'name': 'zhangshuang', 'school': 'MIT', 'sex': 'male'}
  48. In [51]: d.setdefault('salary',25000.00)
  49. Out[51]: 25000.0
  50. In [52]: d
  51. Out[52]:
  52. {'age': 30,
  53. 'name': 'zhangshuang',
  54. 'salary': 25000.0,
  55. 'school': 'MIT',
  56. 'sex': 'male'}
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注