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

字典特性:

  • 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 樱井莉亚
"""
dic = {
"123":"abc",
"456":"dfg",
"789":"hij"
}
print(dic.items())#dict_items([('123', 'abc'), ('456', 'dfg'), ('789', 'hij')])
for key in dic.keys(): #123 遍历字典的key
print(key) #456
#789
for value in dic.values(): #abc 遍历字典的value
print(value) #dfg
#hij
for items in dic.items():
print(items)
# ('123', 'abc')
# ('456', 'dfg')
# ('789', 'hij')
 

清空

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

 

posted @ 2018-02-21 19:48  Kils  阅读(452)  评论(0编辑  收藏  举报