![](/img/trans.png)
[英]How to combine two dictionaries? One dict as key, another dict as value
[英]combine two dictionaries into one, with elements from dict2 taking precedence
def combine_guests(guests1, guests2):
# Combine both dictionaries into one, with each key listed
# only once, and the value from guests1 taking precedence
Rorys_guests = { "Adam":2, "Brenda":3, "David":1, "Jose":3, "Charlotte":2, "Terry":1, "Robert":4}
Taylors_guests = { "David":4, "Nancy":1, "Robert":2, "Adam":1, "Samantha":3, "Chris":5}
print(combine_guests(Rorys_guests, Taylors_guests))
想要这个 output
{"Adam": [2, 1], "Branda": 3, "David": [1, 4] ...}
来自 guests1 ieRorys_guests dict 的值将优先。即对于 Adam 键值是 [2,1]
def combine_guests(guests1, guests2):
#Update function will add new values and update existing values in the dictionary
guests2.update(guests1)
return guests2
Rorys_guests = { "Adam":2, "Brenda":3, "David":1, "Jose":3, "Charlotte":2,
"Terry":1, "Robert":4}
Taylors_guests = { "David":4, "Nancy":1, "Robert":2, "Adam":1, "Samantha":3,
"Chris":5}
print(combine_guests(Rorys_guests, Taylors_guests))
Python 3.5 或以上
z = {**Rorys_guests , **Taylors_guests }
这应该有效:
def combine_guests(guests1, guests2):
out = guests1.copy()
for key, val in guests2.items():
out[key] = val
return out
编辑:复制来宾2而不是来宾1
这可能会有所帮助。 这将优先考虑Rorys_guests
,如果两者都有值,则不会包括来自Taylors_guests
那些。
Taylors_guests.update(Rorys_guests)
return Rorys_guests
试试这个它会工作
def combine_guests(guests1, guests2):
lst ={}
# Combine both dictionaries into one, with each key listed
# only once, and the value from guests1 taking precedence
lst =guests1
for name,friend in guests2.items():
if name in lst:
pass
else:
lst[name] =friend
return lst
Rorys_guests = { "Adam":2, "Brenda":3, "David":1, "Jose":3, "Charlotte":2, "Terry":1, "Robert":4}
Taylors_guests = { "David":4, "Nancy":1, "Robert":2, "Adam":1, "Samantha":3, "Chris":5}
print(combine_guests(Rorys_guests, Taylors_guests))
打开任何类型的建议
def combine_guests(guests1, guests2):
# Combine both dictionaries into one, with each key listed
# only once, and the value from guests1 taking precedence
combine = guests1
for guest, friend in guests2.items():
if guest not in combine:
combine[guest] = friend
return combine
Rorys_guests = { "Adam":2, "Brenda":3, "David":1, "Jose":3, "Charlotte":2, "Terry":1, "Robert":4}
Taylors_guests = { "David":4, "Nancy":1, "Robert":2, "Adam":1, "Samantha":3, "Chris":5}
print(combine_guests(Rorys_guests, Taylors_guests))
enter code here: def combine_guests(guests1, guests2):
empty={}
empty.update(guests1)
empty.update(guests2)
for key,val in guests1.items():
empty[key]=val
return(empty)
def combine_guests(guests1, guests2):
precedence=''
guests2.update(guests1)
for key,value in guests2.items():
if key in guests1:
precedence=value+1
guests2[key]=precedence
return guests2
输出:
{'David': 2, 'Nancy': 1, 'Robert': 5, 'Adam': 3, 'Samantha': 3, 'Chris': 5, 'Brenda': 4, 'Jose': 4, 'Charlotte': 3, 'Terry': 2}
def combine_guests(guests1, guests2):
# Combine both dictionaries into one, with each key listed
# only once, and the value from guests1 taking precedence
guests1.update(guests2)
return guests1
def combine_guests(guests1, guests2):
# Combine both dictionaries into one, with each key listed
# only once, and the value from guests1 taking precedence
for key,value in guests2.items():
if key in guests1:
if guests1[key] < guests2[key]:
guests1[key] = value
else:
guests1[key] = value
return guests1
Rorys_guests = { "Adam":2, "Brenda":3, "David":1, "Jose":3, "Charlotte":2, "Terry":1, "Robert":4}
Taylors_guests = { "David":4, "Nancy":1, "Robert":2, "Adam":1, "Samantha":3, "Chris":5}
print(combine_guests(Rorys_guests, Taylors_guests))
这有点晚了,因为 OP 前段时间提交了他们的问题,但我想我会提交我的答案:
def combine_guests(guests1, guests2):
# Combine both dictionaries into one, with each key listed
# only once, and the value from guests1 taking precedence
guests2.update(guests1)
return guests2
def combine_guests(guests1, guests2):
# Combine both dictionaries into one, with each key listed
# only once, and the value from guests1 taking precedence
grand_list={}
grand_list.update(Rorys_guests)
for names, values in Taylors_guests.items():
if names not in Rorys_guests.keys():
grand_list.setdefault(names,values)
return grand_list
Rorys_guests = { "Adam":2, "Brenda":3, "David":1, "Jose":3, "Charlotte":2, "Terry":1, "Robert":4}
Taylors_guests = { "David":4, "Nancy":1, "Robert":2, "Adam":1, "Samantha":3, "Chris":5}
print(combine_guests(Rorys_guests, Taylors_guests))
ANSWER:
{'Adam': 2, 'Brenda': 3, 'David': 1, 'Jose': 3, 'Charlotte': 2, 'Terry': 1, 'Robert': 4, 'Nancy': 1, 'Samantha': 3, 'Chris': 5}
output : {'Adam': [2, 1], 'Brenda': 3, 'David': [1, 4], 'Jose': 3, 'Charlotte': 2, 'Terry': 1, 'Robert': [4, 2], 'Nancy': 1, 'Samantha': 3, 'Chris': 5}
def combine_guests(guests1, guests2):
# Combine both dictionaries into one, with each key listed
# only once, and the value from guests1 taking precedence
g=guests1
for key,value in guests2.items():
if key in g:
g[key]=[g[key],value]
else:
g[key]=value
return g
Rorys_guests = { "Adam":2, "Brenda":3, "David":1, "Jose":3, "Charlotte":2, "Terry":1, "Robert":4}
Taylors_guests = { "David":4, "Nancy":1, "Robert":2, "Adam":1, "Samantha":3, "Chris":5}
print(combine_guests(Rorys_guests, Taylors_guests))
output: {'Adam': [2, 1], 'Brenda': 3, 'David': [1, 4], 'Jose': 3, 'Charlotte': 2, 'Terry': 1, 'Robert': [4, 2], 'Nancy': 1, 'Samantha': 3, 'Chris': 5}
请注意update()
方法,即dict1.update(dict2)
在这里不起作用,因为它会破坏通过替换内容所需的 output 的目的。
解决方案:
def combine_guests(guests1, guests2):
# Combine both dictionaries into one, with each key listed
# only once, and the value from guests1 taking precedence
for guest,friend in guests2.items():
if guest in guests1:
guests1[guest] = [guests1[guest], friend]
else:
guests1[guest] = friend
return guests1
Rorys_guests = { "Adam":2, "Brenda":3, "David":1, "Jose":3, "Charlotte":2, "Terry":1, "Robert":4}
Taylors_guests = { "David":4, "Nancy":1, "Robert":2, "Adam":1, "Samantha":3, "Chris":5}
print(combine_guests(Rorys_guests, Taylors_guests))
Output:
{'Adam': [2, 1], 'Brenda': 3, 'David': [1, 4], 'Jose': 3, 'Charlotte': 2, 'Terry': 1, 'Robert': [4, 2], 'Nancy': 1, 'Samantha': 3, 'Chris': 5}
def combine_guests(guests1, guests2):
对于 guests2.keys() 中的键:
if key in guests1:
del guests2[key]
guests1.update(guests2)
return(guests1)
Rorys_guests = { "Adam":2, "Brenda":3, "David":1, "Jose":3, "Charlotte":2, "Terry":1, "Robert":4} Taylors_guests = { "David “:4,“南希”:1,“罗伯特”:2,“亚当”:1,“萨曼莎”:3,“克里斯”:5}
打印(组合客人(Rorys_guests,Taylors_guests))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.