作者:Carrie
出处:https://home.cnblogs.com/u/hanjiali
版权:本文版权归作者和博客园共有
转载:欢迎转载,但未经作者同意,必须保留此段声明;必须在文章中给出原文连接;否则必究法律责任

1.字典的操作

        字典一种key - value 的数据类型,使用就像我们上学的字典,通过笔画,字典来查对应页的详细内容。

语法:

info = {
    'student1101' : "hanjiali",
    'student1102' : "hanjiali1",
    'student1103' : "hanjiali2",
}

 

info = {
'student1101' : "hanjiali",
'student1102' : "hanjiali1",
'student1103' : "hanjiali2",
}
print(info)
print("-----------------------------------")
#查(存在的情况下不出错,不存在报错)
print(info["student1101"])
print("-----------------------------------")
#查(标准方法)
print(info.get("student1101"))
print("-----------------------------------")
#试一个人在不在
print("student1104" in info )
#输出T or F
print("-----------------------------------")

print("-----------------------------------")
#
info["student1101"] = "韩佳丽"
print(info)
print("-----------------------------------")
#1(标准删除)
info.pop('student1103')
print(info)
print("-----------------------------------")
#2
del info["student1102"]
print(info)
print("-----------------------------------")

运行结果:

{'student1101': 'hanjiali', 'student1102': 'hanjiali1', 'student1103': 'hanjiali2'}
-----------------------------------
hanjiali
-----------------------------------
hanjiali
-----------------------------------
False
-----------------------------------
{'student1101': '韩佳丽', 'student1102': 'hanjiali1', 'student1103': 'hanjiali2'}
-----------------------------------
{'student1101': '韩佳丽', 'student1102': 'hanjiali1'}
-----------------------------------
{'student1101': '韩佳丽'}
-----------------------------------

 2.字典的嵌套

ar_cata = {
"homework" : {
'stud_1101': ["认真完成"],
'stud_1102': ["很差"],
'stud_1103': ["尽最大努力"],




},

"class" : {
's_1101': ["上课注意力集中"],
's_1102': ["不听课"],
's_1103': ["还可以"],

},

"student" : {

'1101' : "hanjiali",
'1102' : "hanjiali1",
'1103' : "hanjiali2",

}


}
ar_cata["student"]["1103"] = "马瑞龙"
print(ar_cata)
print("-----------------------------------")
#创建新的

ar_cata.setdefault("地区",["内蒙古"])
print(ar_cata)

 运行结果:(尽量不写中文,怕字符转换出错)

{'homework': {'stud_1101': ['认真完成'], 'stud_1102': ['很差'], 'stud_1103': ['尽最大努力']}, 'class': {'s_1101': ['上课注意力集中'], 's_1102': ['不听课'], 's_1103': ['还可以']}, 'student': {'1101': 'hanjiali', '1102': 'hanjiali1', '1103': '马瑞龙'}}
-----------------------------------
{'homework': {'stud_1101': ['认真完成'], 'stud_1102': ['很差'], 'stud_1103': ['尽最大努力']}, 'class': {'s_1101': ['上课注意力集中'], 's_1102': ['不听课'], 's_1103': ['还可以']}, 'student': {'1101': 'hanjiali', '1102': 'hanjiali1', '1103': '马瑞龙'}, '地区': ['内蒙古']}

 3.字典的合并

info = {
    'student1101' : "hanjiali",
    'student1102' : "hanjiali1",
    'student1103' : "hanjiali2",
}

b = {

    'student1101': "hanjiali",
    1 : 3,
    2 : 2

}

info.update(b)
print(info)
#转化成列表
print("---------------------------------------------------------------")
print(info.items())

 结果:

{'student1101': 'hanjiali', 'student1102': 'hanjiali1', 'student1103': 'hanjiali2', 1: 3, 2: 2}
---------------------------------------------------------------
dict_items([('student1101', 'hanjiali'), ('student1102', 'hanjiali1'), ('student1103', 'hanjiali2'), (1, 3), (2, 2)])

 4.字典的循环

info = {
    'student1101' : "hanjiali",
    'student1102' : "hanjiali1",
    'student1103' : "hanjiali2",
}

b = {

    'student1101': "hanjiali",
    1 : 3,
    2 : 2

}

info.update(b)
print(info)
print("---------------------------------------------------------------")
print(info.items())

for i in info :
    print(i,info[i])

输出结果:

student1101 hanjiali
student1102 hanjiali1
student1103 hanjiali2
1 3
2 2

 

 

 

 

 

 

 

 

posted on 2019-08-09 10:54  不吃葡萄楞吐皮  阅读(188)  评论(0编辑  收藏  举报