python的dict()字典数据类型的方法详解以及案例使用

时间:2022-02-25 19:28:55

一、之前的回顾

# int  数字
# str 字符串
# list 列表
# tuple 元组
# dict 字典

字典中最重要的方法

keys()

values()

items()

get

update

字典是以 
"key":"value" 
的方式存储的

1、字典里面是键值对,每个元素之间也是用逗号分隔,是用{}大括号括起来的

字典里面的value可以是任何值,可以无限嵌套列表和元组

布尔值(不能跟0或1一起出现,当key里面没有0和1就可以做key)、列表、字典不能作为字典的key

字典是无序的,每次运行,输出的结果都不是固定的

二、下面了解下字典的常用操作

info = {"k1": "v1",
"k2": "v2"
}
print (info) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py {'k1': 'v1', 'k2': 'v2'}
info = {
"k1": 18,
"k2": True,
"k3":[
11,
[],
(),
22,
33,
{
"kk1":"vv1",
"kk2":"vv2",
"kk3":(11,22,),
}
],
"k4":(11,22,33,44,)
}
print (info) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py {'k2': True, 'k1': 18, 'k3': [11, 22, 33, {'kk3': (11, 22), 'kk2': 'vv2', 'kk1': 'vv1'}], 'k4': ()}

2.数字、字符串、布尔值、元组、都可以做key

info = {
2:"abc",
"k1":"def",
True:"",
(11,22):""
}
print (info) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py {'k1': 'def', True: '', 2: 'abc', (11, 22): ''}

3.列表和字典都不能作为字典的key

(1)列表不能作为字典的key

info = {
2:"abc",
"k1":"def",
True:"",
[11,22]:""
} TypeError: unhashable type: 'list'

(2)字典不能作为字典的key

info = {
2:"abc",
"k1":"def",
True:"",
{"k3":"v3"}:""
} TypeError: unhashable type: 'dict'

4、字典去保存的时候是按照哈希表保存的

5、字典是无序的(发现每次打印结果都不一样,所以字典是无序的)

第一次打印

C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py

{'k2': True, 'k4': (11, 22, 33, 44), 'k1': 18, 'k3': [11, [], (), 22, 33, {'kk2': 'vv2', 'kk1': 'vv1', 'kk3': (11, 22)}]}

第二次打印

C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py

{'k4': (11, 22, 33, 44), 'k1': 18, 'k3': [11, [], (), 22, 33, {'kk1': 'vv1', 'kk2': 'vv2', 'kk3': (11, 22)}], 'k2': True}

6.按照key去取值,比如要取到18这个值应该怎么做(这里就不是索引的的0123这些了)

info = {
"k1": 18,
"k2": True,
"k3":[
11,
[],
(),
22,
33,
{
"kk1":"vv1",
"kk2":"vv2",
"kk3":(11,22,),
}
],
"k4":(11,22,33,44,)
}
v = info["k1"]
print (v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py 18

比如想要拿到99999这个值,就是下面这样写了(因为想拿3这个key对应的value,所以直接就写3这个数字就可以了,不用带双引号)

info = {
"k1": 18,
3: 99999,
"k3": [
11,
[],
(),
22,
33,
{
"kk1": "vv1",
"kk2": "vv2",
"kk3": (11, 22,),
}
],
"k4": (11, 22, 33, 44,)
}
v = info[3]
print(v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py 99999

7、拿到11 这个元素该怎么写

第一步拿到k3这个key,结果就是一个列表了,想要拿到元组里面的那个11,就得先拿列表里面的整个字典的元素,位置是5

v = info["k3"]
print (v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py [11, [], (), 22, 33, {'kk3': (11, 22), 'kk2': 'vv2', 'kk1': 'vv1'}]

拿到列表里面的这个字典之后,要想取到元组里面的11,首先得拿到kk3这个key对应的value

v = info["k3"][5]
print (v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py {'kk2': 'vv2', 'kk3': (11, 22), 'kk1': 'vv1'}

现在拿到这个元组了,想要元组里面的11这个元素就简单了,11位于这个元素的第0个位置

v = info["k3"][5]["kk3"]
print (v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py (11, 22)

拿到元组中的11的这个元素了

v = info["k3"][5]["kk3"][0]
print (v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py 11

合起来就是下面这

info = {
"k1": 18,
3: 99999,
"k3": [
11,
[],
(),
22,
33,
{
"kk1": "vv1",
"kk2": "vv2",
"kk3": (11, 22,),
}
],
"k4": (11, 22, 33, 44,)
}
v = info["k3"][5]["kk3"][0]
print(v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py 11

8.删除字典中的key

info = {
"k1": 18,
3: 99999,
"k3": [
11,
[],
(),
22,
33,
{
"kk1": "vv1",
"kk2": "vv2",
"kk3": (11, 22,),
}
],
"k4": (11, 22, 33, 44,)
}
del info["k1"]
print (info) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py {3: 99999, 'k3': [11, [], (), 22, 33, {'kk1': 'vv1', 'kk2': 'vv2', 'kk3': (11, 22)}], 'k4': (11, 22, 33, 44)}

9.删除kk1这个key

info = {
"k1": 18,
3: 99999,
"k3": [
11,
[],
(),
22,
33,
{
"kk1": "vv1",
"kk2": "vv2",
"kk3": (11, 22,),
}
],
"k4": (11, 22, 33, 44,)
}
del info["k3"][5]["kk1"]
print (info) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py {'k1': 18, 'k3': [11, [], (), 22, 33, {'kk3': (11, 22), 'kk2': 'vv2'}], 3: 99999, 'k4': (11, 22, 33, 44)}

10、字典支持for循环,默认循环的时候只有key

info = {
"k1": 18,
3: 99999,
"k3": [
11,
[],
(),
22,
33,
{
"kk1": "vv1",
"kk2": "vv2",
"kk3": (11, 22,),
}
],
"k4": (11, 22, 33, 44,)
}
for n in info:
print (n) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py k1 3 k3 k4

11.取字典里面的key,字典里面也有一个方法叫keys

info = {
"k1": 18,
3: 99999,
"k3": [
11,
[],
(),
22,
33,
{
"kk1": "vv1",
"kk2": "vv2",
"kk3": (11, 22,),
}
],
"k4": (11, 22, 33, 44,)
}
# for n in info:
# print (n)
v = info.keys()
print (v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py dict_keys(['k1', 3, 'k4', 'k3'])

12.取字典里面的values   有一个方法叫做value,可以结合一下for循环,一个一个的打印出来,为啥只有四行呢,因为key只有4个,这是对应的4个key的4个value

info = {
"k1": 18,
3: 99999,
"k3": [
11,
[],
(),
22,
33,
{
"kk1": "vv1",
"kk2": "vv2",
"kk3": (11, 22,),
}
],
"k4": (11, 22, 33, 44,)
}
for n in info.values():
print (n) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py 99999 [11, [], (), 22, 33, {'kk3': (11, 22), 'kk2': 'vv2', 'kk1': 'vv1'}] (11, 22, 33, 44) 18

13.既想拿到key 又想拿到value(自己写for循环)

info = {
"k1": 18,
3: 99999,
"k3": [
11,
[],
(),
22,
33,
{
"kk1": "vv1",
"kk2": "vv2",
"kk3": (11, 22,),
}
],
"k4": (11, 22, 33, 44,)
}
for n in info.keys():
print (n,info[n]) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py k4 (11, 22, 33, 44) k3 [11, [], (), 22, 33, {'kk2': 'vv2', 'kk3': (11, 22), 'kk1': 'vv1'}] 3 99999 k1 18

14、既拿到key又拿到value,字典有一个方法,items  我们用一下试试

因为是2个值,所以循环的时候,要用两个变量,这样items这个方法才会把key的值赋值给k,把value的值赋值给v,最后在打印k,v

info = {
"k1": 18,
3: 99999,
"k3": [
11,
[],
(),
22,
33,
{
"kk1": "vv1",
"kk2": "vv2",
"kk3": (11, 22,),
}
],
"k4": (11, 22, 33, 44,)
}
for k,v in info.items():
print (k,v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py k1 18 k4 (11, 22, 33, 44) 3 99999 k3 [11, [], (), 22, 33, {'kk2': 'vv2', 'kk1': 'vv1', 'kk3': (11, 22)}]

二、dict字典的方法详解

1、clear(self)

D.clear() -> None.  Remove all items from D.

清空字典

D = {"":"aa","":"bb","":"cc"}
v = D.clear()
print (v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py None

2. copy(self)  浅拷贝

D = {"":"aa","":"bb","":"cc"}
v = D.copy()
print (v)
print (D) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py {'': 'bb', '': 'aa', '': 'cc'} {'': 'bb', '': 'aa', '': 'cc'}

3. fromkeys

根据序列,创建字典,并指定统一一个value作为key的值

Returns a new dict with keys from iterable and values equal to value

@staticmethod # known case   这是静态方法的意
def fromkeys(*args, **kwargs)

静态方法是这样使用的  类名.方法

这个必须传2个参数,第一个参数是用来创建字典的key的,必须是可迭代对象,列表、元组,字符串都可以,第二个参数是这些key的统一的值

第一个参数是列表的方式

v = dict.fromkeys(["name","","woai"],123)
print (v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py {'woai': 123, '': 123, 'name': 123}

第一个参数是元组的方式

v = dict.fromkeys(("what","","woai"),123)
print (v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py {'woai': 123, '': 123, 'what': 123}

第一个参数是字符串的方式

v = dict.fromkeys("name",123)
print (v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py {'m': 123, 'e': 123, 'n': 123, 'a': 123}
4. get(self, k, d=None
获取key的值

D.get(k[,d]) -> D[k] if k in D, else d.  d defaults to None.

我们通过这种方法的话,如果key不存在的话,程序就直接报错了

D = {"11":"aa","22":"bb","33":"cc"}
v = D["111"]
print (v)

KeyError: '111'

get这个方法在取值的时候,直接加key的名字就可以取到key的对应的值

D = {
"k1":"v1",
"k2":"v2"
}
v = D.get("k1")
print(v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py v1

当要取的这个key不存在时,可以指定一个返回值,默认是None

D = {
"k1":"v1",
"k2":"v2"
}
v = D.get("k3")
print(v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py None

当要取的这个key不存在时,指定一个固定的返回值

D = {
"k1":"v1",
"k2":"v2"
}
v = D.get("k3","")
print(v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py 000

5. items(self)

同时拿到key和value
D = {
"k1":"v1",
"k2":"v2"
}
for n in D.items():
print(n) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py ('k2', 'v2') ('k1', 'v1')

6. keys(self)

取出字典中的所有key

D = {
"k1":"v1",
"k2":"v2"
}
for n in D.keys():
print(n) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py k2 k1

7. values(self)

取出字典里面的所有value

D = {
"k1":"v1",
"k2":"v2"
}
for n in D.values():
print(n) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py v2 v1

8. pop(self, k, d=None)

删除指定的key

D = {
"k1":"v1",
"k2":"v2"
}
v = D.pop("k1")
print (D) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py {'k2': 'v2'}

删除指定的key并把删除的value打印出来

D = {
"k1":"v1",
"k2":"v2"
}
v = D.pop("k1") v就是指定删除的key 值是v1
print (D,v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py {'k2': 'v2'} v1

当删除的指定的key不存在的时候,可以指定一个参数,让结果返回这个指定的参数

D = {
"k1":"v1",
"k2":"v2"
}
v = D.pop("k3","")
print (D)
print (v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py {'k1': 'v1', 'k2': 'v2'} 000

9.popitem(self)

随机删除一个key

被删除的key和value是以一个元组的方式输出的

D = {
"k1":"v1",
"k2":"v2"
}
v = D.popitem()
print (D)
print (v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py {'k2': 'v2'} ('k1', 'v1')

也可以这样写

D = {
"k1":"v1",
"k2":"v2"
}
k,v = D.popitem()
print (D,k,v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py {'k2': 'v2'} k1 v1

10. setdefault(self, k, d=None)

设置key对应的值,

如果参数里面的key已存在,则不设置,获取当前key对应的值

如果参数里面的key不存在,则设置key的值,获取当前key对应的值

当key存在的时候,则不设置,并且获取当前key的值

D = {
"k1":"v1",
"k2":"v2"
}
v = D.setdefault("k1","woaini")
print (D)
print (v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py {'k2': 'v2', 'k1': 'v1'} v1

当key不存在的时候,则增加一个key,并设置key的值,并返回参数中指定key的值

D = {
"k1":"v1",
"k2":"v2"
}
v = D.setdefault("k3","woaini")
print (D)
print (v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py {'k3': 'woaini', 'k2': 'v2', 'k1': 'v1'} woaini

11. update(self, E=None, **F)

如果key存在的话,就更新key的值,如果key不存在的话,就增加到字典里面

k1是在原来基础上修改的,k3是新增的

D = {
"k1":"v1",
"k2":"v2"
}
D.update({"k1":"","k3":""})
print (D) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py {'k3': '', 'k2': 'v2', 'k1': ''}

第二种写法:

D = {
"k1":"v1",
"k2":"v2"
}
D.update(k1=111,k3=666,k5="woaini")
print (D) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py {'k2': 'v2', 'k5': 'woaini', 'k1': 111, 'k3': 666}

D.update(k1=111,k3=666,k5="woaini")

相当于在内部自动转换成一个字典,做个更新

如果参数里面是**kwargs  这样的,默认就会把

D.update(k1=111,k3=666,k5="woaini") 自动转换成字典

12.字典里面的in操作

(1)判断字典的key里面是否包含k1

只能判断key不能判断vlaue,想判断value怎么办呢,看(2)

D = {
"k1":"v1",
"k2":"v2"
}
v = "k1" in D
print (v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py True

(2)判断字典里面的value是否包含v1

D = {
"k1":"v1",
"k2":"v2"
}
v = "v1" in D.values()
print (v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py True