字典#

视频

使用 键值对 的形式创建:

d = {'wang': '111', 'U': '123'}
d
{'wang': '111', 'U': '123'}

使用 函数 dict() 创建:

  • 传入键值对位置参数:

d1 = dict([('A', 1), ('B', 2)])
d1
{'A': 1, 'B': 2}
  • 以关键字参数传入:

d2 = dict(A=1, B=2)
d2
{'A': 1, 'B': 2}

也可以是另一个字典作为参数:

d3 = dict(d2)
d3
{'A': 1, 'B': 2}

或者使用字典解包:

{**d3, 'c': 3}
{'A': 1, 'B': 2, 'c': 3}
  • len(字典) 返回字典的项数

  • in(类似于序列的用法)

len(d3) # 字典的大小
2
'A' in d3 # 判断是否存在某个元素
True

通过键查找或修改#

示例:

d = {'wang': '111', 'U': '123'}
d['U'] # 查找元素
'123'
d['wang'] = 111
d # 修改元素
{'wang': 111, 'U': '123'}
del d['wang'] # 显式删除
d 
{'U': '123'}

clear():清除字典中所有的项

d = {'wang': '111', 'U': '123'}
d.clear()
d
{}

update 使用新字典更新旧字典#

新字典中有而旧字典中没有的项会被加入到旧字典中;新字典中有而旧字典中也有的值会被新字典的值所代替。

d1 = {'n':'xx','p':'110'}
d2 = {'p':'120','a':'A'}
d1.update(d2)
d1
{'n': 'xx', 'p': '120', 'a': 'A'}
d2
{'p': '120', 'a': 'A'}

复制 copy()#

浅复制,得到一个键的指向完全相同原字典的副本。

d = {'wang':'111','U':[1,2,3,4]}
d1 = d.copy()
d1
{'wang': '111', 'U': [1, 2, 3, 4]}

原地修改原字典 d,相应的 d1 也会被修改,反之亦然。

d1['U'].append('lr')
d1
{'wang': '111', 'U': [1, 2, 3, 4, 'lr']}
d
{'wang': '111', 'U': [1, 2, 3, 4, 'lr']}

如果使用 deepcopy() 函数则可以避免上述情况发生。

from copy import deepcopy

d = {'wang':'111','U':[1,2,3,4]}

d1 = deepcopy(d)
d1
{'wang': '111', 'U': [1, 2, 3, 4]}
d
{'wang': '111', 'U': [1, 2, 3, 4]}
d1['U'].append('lr')
d1
{'wang': '111', 'U': [1, 2, 3, 4, 'lr']}
d
{'wang': '111', 'U': [1, 2, 3, 4]}

get 方法,查找元素#

如若元素不存在,可以自定义返回的内容(默认为 None):

d = {}
d.get('name')
d
{}
d['name'] = 'Tom'
d
{'name': 'Tom'}
d.get('name')
'Tom'
d.get('phone','Unknown')
'Unknown'

setdefault 方法,查找元素#

get 方法不同的是,当键不存在时,自定义的值和该键会组成一个新项被加入字典。

d
{'name': 'Tom'}
d.setdefault('phone','119')
'119'
d
{'name': 'Tom', 'phone': '119'}

items()keys()values()#

均以列表的形式返回 a set-like object,其中的元素分别为”项”,“键”,“值”。

d = {'wang':'111', 'U':[1,2,3,4]}
d
{'wang': '111', 'U': [1, 2, 3, 4]}
d.items()
dict_items([('wang', '111'), ('U', [1, 2, 3, 4])])
d.keys()
dict_keys(['wang', 'U'])
d.values()
dict_values(['111', [1, 2, 3, 4]])

pop(键)#

返回键对应的值,并删除字典中这个键对应的项。

d = {'wang':'111','U':[1,2,3,4]}
d.pop('U')
[1, 2, 3, 4]
d
{'wang': '111'}

popitem()#

随机返回字典中的项,并从字典中删除。

d = {'wang':'111', 'U':[1,2,3,4]}
d.popitem()
('U', [1, 2, 3, 4])
d
{'wang': '111'}

打印乘法表#

for 语句#

# 定义乘数
table = {1, 2, 3, 4, 5, 6 ,7, 8, 9}

def multiply(x, y, /):
    return x * y

def print_output(x, y, /):
    out = f"{x} x {y} = {multiply(x, y)}"
    print(out, end='|')
for x in table:
    for y in table:
        print_output(x, y)
    print('\n')
1 x 1 = 1|1 x 2 = 2|1 x 3 = 3|1 x 4 = 4|1 x 5 = 5|1 x 6 = 6|1 x 7 = 7|1 x 8 = 8|1 x 9 = 9|

2 x 1 = 2|2 x 2 = 4|2 x 3 = 6|2 x 4 = 8|2 x 5 = 10|2 x 6 = 12|2 x 7 = 14|2 x 8 = 16|2 x 9 = 18|

3 x 1 = 3|3 x 2 = 6|3 x 3 = 9|3 x 4 = 12|3 x 5 = 15|3 x 6 = 18|3 x 7 = 21|3 x 8 = 24|3 x 9 = 27|

4 x 1 = 4|4 x 2 = 8|4 x 3 = 12|4 x 4 = 16|4 x 5 = 20|4 x 6 = 24|4 x 7 = 28|4 x 8 = 32|4 x 9 = 36|

5 x 1 = 5|5 x 2 = 10|5 x 3 = 15|5 x 4 = 20|5 x 5 = 25|5 x 6 = 30|5 x 7 = 35|5 x 8 = 40|5 x 9 = 45|

6 x 1 = 6|6 x 2 = 12|6 x 3 = 18|6 x 4 = 24|6 x 5 = 30|6 x 6 = 36|6 x 7 = 42|6 x 8 = 48|6 x 9 = 54|

7 x 1 = 7|7 x 2 = 14|7 x 3 = 21|7 x 4 = 28|7 x 5 = 35|7 x 6 = 42|7 x 7 = 49|7 x 8 = 56|7 x 9 = 63|

8 x 1 = 8|8 x 2 = 16|8 x 3 = 24|8 x 4 = 32|8 x 5 = 40|8 x 6 = 48|8 x 7 = 56|8 x 8 = 64|8 x 9 = 72|

9 x 1 = 9|9 x 2 = 18|9 x 3 = 27|9 x 4 = 36|9 x 5 = 45|9 x 6 = 54|9 x 7 = 63|9 x 8 = 72|9 x 9 = 81|

制作可查询的乘法表:

def output(x, y, /):
    out = f"{x} x {y} = {multiply(x, y):2d}"
    return out
# 字典推导式
D = {
    f"{x},{y}": output(x, y) 
    for x in table for y in table
}
D['1,3']
'1 x 3 =  3'
D['5,3']
'5 x 3 = 15'