![](/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.