Python11:字典

字典也是一种数据类型,key-value的数据类型。

字典的特性:dict是无序的,key必须是唯一的。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

dict = {
    "str01":"one",
    "str02":"two",
    "str03":"three"
}
print(dict)

输出:

{'str01': 'one', 'str03': 'three', 'str02': 'two'}

Process finished with exit code 0

解释:

定义并输出字典。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

dict = {
    "str01":"one",
    "str02":"two",
    "str03":"three"
}
print(dict.items())

输出:

dict_items([('str03', 'three'), ('str01', 'one'), ('str02', 'two')])

Process finished with exit code 0

解释:

dict.items():将字典转成列表。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

dict = {
    "str01":"one",
    "str02":"two",
    "str03":"three"
}
dict1 = {
    "str01":"Python",
    "str04":"java",
    "str05":"php"
}
dict.update(dict1)
print(dict)

输出:

{'str05': 'php', 'str02': 'two', 'str01': 'Python', 'str04': 'java', 'str03': 'three'}

Process finished with exit code 0

解释:

dict.update(dict1):更新字典,有相同的键就更新,没有相同的键就合并。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

c = dict.fromkeys([1,2,3],"test")
print(c)

输出:

{1: 'test', 2: 'test', 3: 'test'}

Process finished with exit code 0

解释:

c = dict.fromkeys([1,2,3],"test"):创建一个新的字典,键是前边的值 ,值是后边的值。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

dict = {
    "str01":"one",
    "str02":"two",
    "str03":"three"
}
print(dict["str01"])

输出:

one

Process finished with exit code 0

解释:

print(dict["str01"]):字典查找,但除非确定有这个键,可以这样精确查找,否则可以用下面这种方法。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

dict = {
    "str01":"one",
    "str02":"two",
    "str03":"three"
}
print(dict.get("str02"))

输出:

two

Process finished with exit code 0

解释:

print(dict.get("str02")):字典的查找。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

dict = {
    "str01":"one",
    "str02":"two",
    "str03":"three"
}
print("str03" in dict)

输出:

True

Process finished with exit code 0

解释:

print("str03" in dict):判断一个key 是否在字典里。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

dict = {
    "str01":"one",
    "str02":"two",
    "str03":"three"
}
dict["str01"] = "oneone"
print(dict.get("str01"))

输出:

oneone

Process finished with exit code 0

解释:

dict["str01"] = "oneone"字典的修改。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

dict = {
    "str01":"one",
    "str02":"two",
    "str03":"three"
}
del dict["str01"]
print(dict)

输出:

{'str02': 'two', 'str03': 'three'}

Process finished with exit code 0

解释:

del dict["str01"]删除字典的键值对。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

dict = {
    "str01":"one",
    "str02":"two",
    "str03":"three"
}
dict["str04"] = "four"
print(dict)

输出:

{'str03': 'three', 'str01': 'one', 'str02': 'two', 'str04': 'four'}

Process finished with exit code 0

解释:

dict["str04"] = "four"如果字典原来没有这个键,哪就不是修改,而是创建新的键值对。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

dict = {
    "str01":"one",
    "str02":"two",
    "str03":"three"
}
dict.pop("str01")
print(dict)

输出:

{'str02': 'two', 'str03': 'three'}

Process finished with exit code 0

解释:

 dict.pop("str01")字典的删除操作,字典的pop()方法必须传参,参数是字典的键。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

dict = {
    "str01":"one",
    "str02":"two",
    "str03":"three"
}
dict.popitem()
print(dict)

输出:

{'str01': 'one', 'str02': 'two'}

Process finished with exit code

解释:

dict.popitem():字典的随机删除一个键值对。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

grade = {
    "一班":{
        "str01":["HanMei",5],
        "str02":["LiLei",6]
    },
    "二班":{
        "str01":["Jack",6],
        "str02":["Rose",7]
    },
    "三班":{
        "str01":["Danni",7],
        "str02":["Rui",8]
    }
}
grade["三班"]["str02"][1] = 10
grade.setdefault("四班",{"str01":["Blue",9]})
print(grade)

输出:

{'一班': {'str01': ['HanMei', 5], 'str02': ['LiLei', 6]}, '四班': {'str01': ['Blue', 9]}, '三班': {'str01': ['Danni', 7], 'str02': ['Rui', 10]}, '二班': {'str01': ['Jack', 6], 'str02': ['Rose', 7]}}

Process finished with exit code 0

解释:

grade["三班"]["str02"][1] = 10:多级字典的修改。

grade.setdefault("四班",{"str01":["Blue",9]}):多级字典中,如果没有键,则创建。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

dict = {
    "str01":"one",
    "str02":"two",
    "str03":"three"
}
for i in dict:
    print(i,dict[i])

输出:

str01 one

str03 three

str02 two

Process finished with exit code 0

解释:

字典的遍历输出。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

dict = {
    "str01":"one",
    "str02":"two",
    "str03":"three"
}
for key,val in dict.items():
    print(key,val)

输出:

str03 three

str02 two

str01 one

Process finished with exit code 0

解释:

字典的遍历输出,但这种遍历方法不如上一种高效,因为如果字典有很多键值对的时候,它会开辟很多的内存空间,严重影响效率。

原文地址:https://www.cnblogs.com/mclind/p/8848520.html