码迷,mamicode.com
首页 > 编程语言 > 详细

Python学习之路-字典dict常用方法

时间:2018-02-21 20:04:45      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:pre   ffffff   常用方法   col   auth   values   body   upd   div   

字典特性:

  • dict无序
  • key唯一,天生去重

创建字典:

way1:小心列表坑

 

 1 __author__ = "KuanKuan"
 2 d = dict.fromkeys([1, 2, 3], ["name", "age"])
 3 print("字典:", d)
 4 
 5 d[1][0] = "mmph好坑"
 6 print("修改后:",d)
 7 """
 8 输出:
 9 字典: {1: [‘name‘, ‘age‘], 2: [‘name‘, ‘age‘], 3: [‘name‘, ‘age‘]}
10 修改后: {1: [‘mmph好坑‘, ‘age‘], 2: [‘mmph好坑‘, ‘age‘], 3: [‘mmph好坑‘, ‘age‘]}
11 
12 """

 

way2:

 1 info = {"teacher1":"苍井空",
 2         "teacher2":"京香",
 3         "teacher3":"波多野结衣",
 4         "teacher4":"小泽玛利亚"
 5         }
 6 print("你懂得:",info)
 7 """
 8 输出:
 9 你懂得: {‘teacher1‘: ‘苍井空‘, ‘teacher2‘: ‘京香‘, ‘teacher3‘: ‘波多野结衣‘, ‘teacher4‘: ‘小泽玛利亚‘}
10 """

字典无序输出

 

查询

print(info["teacher1"])#如果不存在会报错
print(info.get("teacher5"))#推荐用这种方式如果key不存在则返回None
print("teacher1" in info)#如果存在返回True
print("查询字典的keys:",info.keys())#查询字典的keys
print("查询字典的values:",info.values())#查询字典的values
print(info.items())
"""
苍井空
None
True
查询字典的keys: dict_keys([‘teacher1‘, ‘teacher2‘, ‘teacher3‘, ‘teacher4‘])
查询字典的values: dict_values([‘苍井空‘, ‘京香‘, ‘波多野结衣‘, ‘小泽玛利亚‘])
dict_items([(‘teacher1‘, ‘苍井空‘), (‘teacher2‘, ‘京香‘), (‘teacher3‘, ‘波多野结衣‘), (‘teacher4‘, ‘小泽玛利亚‘)])
"""

修改

1 info["teacher1"] = "天海翼"
2 print("修改后:",info)
3 #修改后: {‘teacher1‘: ‘天海翼‘, ‘teacher2‘: ‘京香‘, ‘teacher3‘: ‘波多野结衣‘, ‘teacher4‘: ‘小泽玛利亚‘}

增加

 1 info["teacher1"] = "天海翼"
 2 print("修改后:",info)
 3 #修改后: {‘teacher1‘: ‘天海翼‘, ‘teacher2‘: ‘京香‘, ‘teacher3‘: ‘波多野结衣‘, ‘teacher4‘: ‘小泽玛利亚‘}
 4 info["teacher5"] = "上原瑞惠"#没有则自动创立
 5 print("增加后的字典:",info)
 6 info.setdefault("teacher1","樱井莉亚")#存在则不会被修改
 7 info.setdefault("teacher6","樱井莉亚")
 8 print(info)
 9 b = {"teacher1":"苍井空",
10      "teacher7":"桃谷绘里香"
11      }
12 info.update(b)
13 print("修改后的:",info)
14 """
15 修改后: {‘teacher1‘: ‘天海翼‘, ‘teacher2‘: ‘京香‘, ‘teacher3‘: ‘波多野结衣‘, ‘teacher4‘: ‘小泽玛利亚‘}
16 增加后的字典: {‘teacher1‘: ‘天海翼‘, ‘teacher2‘: ‘京香‘, ‘teacher3‘: ‘波多野结衣‘, ‘teacher4‘: ‘小泽玛利亚‘, ‘teacher5‘: ‘上原瑞惠‘}
17 {‘teacher1‘: ‘天海翼‘, ‘teacher2‘: ‘京香‘, ‘teacher3‘: ‘波多野结衣‘, ‘teacher4‘: ‘小泽玛利亚‘, ‘teacher5‘: ‘上原瑞惠‘, ‘teacher6‘: ‘樱井莉亚‘}
18 修改后的: {‘teacher1‘: ‘苍井空‘, ‘teacher2‘: ‘京香‘, ‘teacher3‘: ‘波多野结衣‘, ‘teacher4‘: ‘小泽玛利亚‘, ‘teacher5‘: ‘上原瑞惠‘, ‘teacher6‘: ‘樱井莉亚‘, ‘teacher7‘: ‘桃谷绘里香‘}
19 """

删除

 1 del info["teacher1"]
 2 print("删除后的:",info)
 3 info.pop("teacher2")
 4 print("删除后的2:",info)
 5 info.popitem()
 6 print("随机删除后的:",info)
 7 """
 8 删除后的: {‘teacher2‘: ‘京香‘, ‘teacher3‘: ‘波多野结衣‘, ‘teacher4‘: ‘小泽玛利亚‘, ‘teacher5‘: ‘上原瑞惠‘, ‘teacher6‘: ‘樱井莉亚‘, ‘teacher7‘: ‘桃谷绘里香‘}
 9 删除后的2: {‘teacher3‘: ‘波多野结衣‘, ‘teacher4‘: ‘小泽玛利亚‘, ‘teacher5‘: ‘上原瑞惠‘, ‘teacher6‘: ‘樱井莉亚‘, ‘teacher7‘: ‘桃谷绘里香‘}
10 随机删除后的: {‘teacher3‘: ‘波多野结衣‘, ‘teacher4‘: ‘小泽玛利亚‘, ‘teacher5‘: ‘上原瑞惠‘, ‘teacher6‘: ‘樱井莉亚‘}
11 
12 """

遍历

for i in info:
    print(i,info[i])
print("*"*50)
for key,value in info.items():#先转列表,在遍历,如果字典过大,会变慢
    print(key,value)
"""
teacher3 波多野结衣
teacher4 小泽玛利亚
teacher5 上原瑞惠
teacher6 樱井莉亚
**************************************************
teacher3 波多野结衣
teacher4 小泽玛利亚
teacher5 上原瑞惠
teacher6 樱井莉亚
"""

清空

1 info.clear()
2 print("清空后的:",info)
3 #清空后的: {}

 

Python学习之路-字典dict常用方法

标签:pre   ffffff   常用方法   col   auth   values   body   upd   div   

原文地址:https://www.cnblogs.com/JankinYu/p/8457107.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!