鱼C论坛

 找回密码
 立即注册
查看: 3217|回复: 0

[学习笔记] 026字典:当索引不好用时2

[复制链接]
发表于 2017-7-2 23:23:54 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
1.dict()不是一个BIF,是一个工厂函数(类型);
  1. >>> dict1 = {}           #定义一个空的字典
  2. >>> dict1.fromkeys((1, 2, 3))       #不提供第二个参数时,默认为None
  3. {1: None, 2: None, 3: None}
  4. >>> dict1.fromkeys((1, 2, 3), 'Number')   #第二个参数设置为Number时
  5. {1: 'Number', 2: 'Number', 3: 'Number'}
  6. >>> dict1.fromkeys((1, 2, 3), ('one', 'two', 'three'))#第二个参数设置为('one', 'two', 'three')时
  7. {1: ('one', 'two', 'three'), 2: ('one', 'two', 'three'), 3: ('one', 'two', 'three')}
  8. >>> dict1.fromkeys((1, 3), '数字')      #重新创建了新的字典
  9. {1: '数字', 3: '数字'}
  10. >>> dict1 = dict1.fromkeys(range(32), '赞')      #创建新的字典
  11. >>> dict1
  12. {0: '赞', 1: '赞', 2: '赞', 3: '赞', 4: '赞', 5: '赞', 6: '赞', 7: '赞', 8: '赞', 9: '赞', 10: '赞', 11: '赞', 12: '赞', 13: '赞', 14: '赞', 15: '赞', 16: '赞', 17: '赞', 18: '赞', 19: '赞', 20: '赞', 21: '赞', 22: '赞', 23: '赞', 24: '赞', 25: '赞', 26: '赞', 27: '赞', 28: '赞', 29: '赞', 30: '赞', 31: '赞'}
  13. >>> for eachKey in dict1.keys():  
  14.         print(eachKey)              #打印字典中所有的Key

  15.        
  16. 0
  17. 1
  18. 2
  19. 3
  20. 4
  21. 5
  22. 6
  23. 7
  24. 8
  25. 9
  26. 10
  27. 11
  28. 12
  29. 13
  30. 14
  31. 15
  32. 16
  33. 17
  34. 18
  35. 19
  36. 20
  37. 21
  38. 22
  39. 23
  40. 24
  41. 25
  42. 26
  43. 27
  44. 28
  45. 29
  46. 30
  47. 31
  48. >>> for eachValue in dict1.values():
  49.         print(eachValue)        #打印字典中所有的Value


  50.        
































  51. >>> for eachItem in dict1.items():
  52.         print(eachItem)       #打印字典中所有的项Item

  53.        
  54. (0, '赞')
  55. (1, '赞')
  56. (2, '赞')
  57. (3, '赞')
  58. (4, '赞')
  59. (5, '赞')
  60. (6, '赞')
  61. (7, '赞')
  62. (8, '赞')
  63. (9, '赞')
  64. (10, '赞')
  65. (11, '赞')
  66. (12, '赞')
  67. (13, '赞')
  68. (14, '赞')
  69. (15, '赞')
  70. (16, '赞')
  71. (17, '赞')
  72. (18, '赞')
  73. (19, '赞')
  74. (20, '赞')
  75. (21, '赞')
  76. (22, '赞')
  77. (23, '赞')
  78. (24, '赞')
  79. (25, '赞')
  80. (26, '赞')
  81. (27, '赞')
  82. (28, '赞')
  83. (29, '赞')
  84. (30, '赞')
  85. (31, '赞')
  86. >>> print(dict1[31])

  87. >>> print(dict1[32])        #程序报错
  88. Traceback (most recent call last):
  89.   File "<pyshell#18>", line 1, in <module>
  90.     print(dict1[32])
  91. KeyError: 32
  92. >>> dict1.get(32)           #访问字典中不存在的项
  93. >>> print(dict1.get(32))
  94. None
  95. >>> dict1.get(32, '木有!')  #访问字典中不存在的项
  96. '木有!'
  97. >>> dict1.get(31, '木有!')  #访问字典中的项
  98. '赞'
  99. >>> 31 in dict1       #成员字典操作符,查找的是键而非值
  100. True
  101. >>> 32 in dict1       #成员字典操作符,查找的是键而非值
  102. False
  103. >>> dict1
  104. {0: '赞', 1: '赞', 2: '赞', 3: '赞', 4: '赞', 5: '赞', 6: '赞', 7: '赞', 8: '赞', 9: '赞', 10: '赞', 11: '赞', 12: '赞', 13: '赞', 14: '赞', 15: '赞', 16: '赞', 17: '赞', 18: '赞', 19: '赞', 20: '赞', 21: '赞', 22: '赞', 23: '赞', 24: '赞', 25: '赞', 26: '赞', 27: '赞', 28: '赞', 29: '赞', 30: '赞', 31: '赞'}
  105. >>> dict1.clear()      #清空字典
  106. >>> dict1
  107. {}
  108. >>> dict1 = {}     
  109. >>> a = {'姓名':'小甲鱼'}
  110. >>> b = a              
  111. >>> b
  112. {'姓名': '小甲鱼'}
  113. >>> a = {}        # a清空了
  114. >>> a
  115. {}
  116. >>> b             # b未清空
  117. {'姓名': '小甲鱼'}
  118. >>> a = b
  119. >>> a
  120. {'姓名': '小甲鱼'}
  121. >>> b
  122. {'姓名': '小甲鱼'}
  123. >>> a.clear()
  124. >>> a            # a清空
  125. {}
  126. >>> b            # b清空
  127. {}
  128. >>> a = {1:'one', 2:'two', 3:'three'}
  129. >>> b = a.copy()         
  130. >>> c = a
  131. >>> c
  132. {1: 'one', 2: 'two', 3: 'three'}
  133. >>> a
  134. {1: 'one', 2: 'two', 3: 'three'}
  135. >>> b
  136. {1: 'one', 2: 'two', 3: 'three'}
  137. >>> id(a)      
  138. 38270128
  139. >>> id(b)        #拷贝后的地址与原地址不一样
  140. 35828672
  141. >>> id(c)        #赋值后的地址与原地址一样
  142. 38270128
  143. >>> c[4] = 'four'  #c增加一个值
  144. >>> c
  145. {1: 'one', 2: 'two', 3: 'three', 4: 'four'}
  146. >>> a
  147. {1: 'one', 2: 'two', 3: 'three', 4: 'four'}
  148. >>> b
  149. {1: 'one', 2: 'two', 3: 'three'}
  150. >>> a.pop(2)
  151. 'two'
  152. >>> a
  153. {1: 'one', 3: 'three', 4: 'four'}
  154. >>> a.popitem()                   #随机从字典中去除一个元素
  155. (4, 'four')
  156. >>> a
  157. {1: 'one', 3: 'three'}
  158. >>> a.setdefault('小白')       # 找不到对应值时,会自动进行添加
  159. >>> a
  160. {1: 'one', 3: 'three', '小白': None}
  161. >>> a.setdefault(5, 'five')
  162. 'five'
  163. >>> a
  164. {1: 'one', 3: 'three', '小白': None, 5: 'five'}     #添加以后自动排序
  165. >>> b = {'小白':'狗'}
  166. >>> a.update(b)       #利用一个字典或映射关系去更新另外一个字典     
  167. >>> a
  168. {1: 'one', 3: 'three', '小白': '狗', 5: 'five'}
复制代码

评分

参与人数 1鱼币 +1 收起 理由
小甲鱼 + 1

查看全部评分

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 23:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表