文章目录
1、合并两个字典
- 方法一:
def merge_two_dicts(a, b):
c = a.copy() # make a copy of a
c.update(b) # modify keys and values of a with the once from b
return c
a = {
'x':1,'y':14}
b = {
'y':314,'z':5, 'a':2, 'b':'0'}
v1 = merge_two_dicts(a,b)
print(v1)
- 方法二:
def merge_dictionaries(a, b):
return {
**a, **b}
a = {
'x': 1, 'y': 2}
b = {
'y': 3, 'z': 4}
print(merge_dictionaries(a, b))
2、将两个列表转化为字典(zip方法)
def to_dictionary(keys, values):
return dict