发表于 2018-8-12 09:35:04

python 字典操作

#values  
>>> info.values()
  
dict_values(['LongZe Luola', 'XiaoZe Maliya'])
  

  
#keys
  
>>> info.keys()
  
dict_keys(['stu1102', 'stu1103'])
  

  

  
#setdefault
  
>>> info.setdefault("stu1106","Alex")
  
'Alex'
  
>>> info
  
{'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1106': 'Alex'}
  
>>> info.setdefault("stu1102","龙泽萝拉")
  
'LongZe Luola'
  
>>> info
  
{'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1106': 'Alex'}
  

  

  
#update
  
>>> info
  
{'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1106': 'Alex'}
  
>>> b = {1:2,3:4, "stu1102":"龙泽萝拉"}
  
>>> info.update(b)
  
>>> info
  
{'stu1102': '龙泽萝拉', 1: 2, 3: 4, 'stu1103': 'XiaoZe Maliya', 'stu1106': 'Alex'}
  

  
#items
  
info.items()
  
dict_items([('stu1102', '龙泽萝拉'), (1, 2), (3, 4), ('stu1103', 'XiaoZe Maliya'), ('stu1106', 'Alex')])
  

  

  
#通过一个列表生成默认dict,有个没办法解释的坑,少用吧这个
  
>>> dict.fromkeys(,'testd')
  
{1: 'testd', 2: 'testd', 3: 'testd'}
页: [1]
查看完整版本: python 字典操作