创建字典后的数据格式

字典里没有顺序的概念

  1. {'姓名':'张三'}
  2. {1: 'one', 2: 'two', 3: 'three', 4: 'four'}
  3. {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: '赞
  4. ', 30: '赞', 31: '赞'}

创建字典

  1. dictStr = {1:'one', 2:'two', 3:'three', 4:'four'}
  2. print(dictStr)
  3. # 打印
  4. {1: 'one', 2: 'two', 3: 'three', 4: 'four'}
  5. # 访问对应的值
  6. print(dictStr[2])
  7. # 打印
  8. two

创建空的字典

  1. dictStr = {}
  2. print(dictStr)
  3. # 打印
  4. {}

实现索引

  1. jsondata = {'name': '张三', 'age':18}
  2. print(jsondata['name'])
  3. #打印
  4. 张三
  1. brand = ['张三', '李四', '王五', '李白']
  2. age = ['18', '20', '28', '10']
  3. print(age[brand.index('张三')])
  4. # 打印
  5. 18

创建字典 dict()

dict(( ))

元组的方式

  1. ##### 这里使用的是元组的方式
  2. dictStr = dict(((1,'one'),(2,'two'),(3,'three'),(4,'four')))
  3. print(dictStr)
  4. # 打印
  5. {1: 'one', 2: 'two', 3: 'three', 4: 'four'}

列表的方式

  1. dictStr2 = dict(([1,'one'],[2,'two'],[3,'three'],[4,'four']))
  2. print(dictStr2)
  3. # 打印
  4. {1: 'one', 2: 'two', 3: 'three', 4: 'four'}

关键字的方式

  1. dictStr3 = dict(张三 = '你好世界', 苍井空='让av征服全世界')
  2. print(dictStr3['张三'])
  3. # 打印
  4. 你好世界

修改键值

  1. dictStr3 = dict(张三 = '你好世界', 苍井空='让av征服全世界')
  2. dictStr2['张三'] = 'abcd'
  3. print(dictStr3['张三'])
  4. # 打印
  5. abcd

新增键值

  1. dictStr3 = dict(张三 = '你好世界', 苍井空='让av征服全世界')
  2. dictStr2['李四'] = '飞流直下三千尺'
  3. print(dictStr3)
  4. # 打印
  5. {'张三': '你好世界', '苍井空': '让av征服全世界', '李四': '飞流直下三千尺'}

访问字典 .fromkeys()

  1. dict1 = {} # 初始化一个字典
  2. dict1 = dict1.fromkeys((1, 2, 3))
  3. dict1 = dict1.fromkeys((1, 2, 3), 'Number')
  4. # 打印
  5. {1: None, 2: None, 3: None}
  6. {1: 'Number', 2: 'Number', 3: 'Number'}
  • keys() 打印 索引值
  • values() 打印value的值
  • items() 整个元素打印出现 ```python dict1 = {} # 初始化一个字典

dict1 = dict1.fromkeys(range(32), ‘赞’) print(dict1)

打印

{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: ‘赞’}

for eachkey in dict1.keys(): print(eachkey) # 打印: 0 1 2

for eachvalue in dict1.values(): print(eachvalue) # 打印: 赞 赞 赞

for eachitem in dict1.items(): print(eachitem) # 打印: (0, ‘赞’) (1, ‘赞’)

  1. <a name="j4Abj"></a>
  2. ### 访问超出下标值 .get()
  3. ```python
  4. dict1 = dict1.fromkeys(range(32), '赞')
  5. print(dict1[31]) # 0 ~ 31 # 显示正常 赞
  6. print(dict1[32]) # 报错 KeyError: 32
  7. print(dict1.get(32)) # None
  8. print(dict1.get(32, '木有!')) # 如果有值的话就会打印值, 如果没有的话会打印 木有!
  9. print(dict1.get(31, '木有!')) # 打印 赞

返回最后一个值 .popitem()

  1. a = {1: 'one', 2: 'two', 3: 'three', 4: 'four'}
  2. print(a.popitem())
  3. # 打印
  4. 4: 'four'

查找键,没有就添加键 .setdefault()

查找键, 如果查找的键不存在就会插入该查找的键

  1. a = {1: 'one', 2: 'two', 3: 'three'}
  2. print(a.setdefault(3))
  3. print(a.setdefault('张三'))
  4. print(a.setdefault(4,'张三'))
  5. # 打印
  6. three
  7. {1: 'one', 2: 'two', 3: 'three', '张三': None}
  8. {1: 'one', 2: 'two', 3: 'three', 4: '张三'}

清空字典 .clear()

  1. dict1 = dict1.fromkeys(range(32), '赞')
  2. dict1.clear()
  3. # 打印
  4. None

删除值 .pop()

参数 给一个键 返回删除对应的值

  1. a = {1: 'one', 2: 'two', 3: 'three', 4: 'four'}
  2. print(a.pop(1))
  3. # 打印
  4. one

更新 .update()

  1. a = {1: 'one', 2: 'two', 3: 'three'}
  2. print(a)
  3. b = {3: '李四'}
  4. a.update(b)
  5. print(a)
  6. # 打印
  7. {1: 'one', 2: 'two', 3: 'three'}
  8. {1: 'one', 2: 'two', 3: '李四'}