简体   繁体   English

将两个词典合二为一,dict2 中的元素优先

[英]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))

Want this output想要这个 output

{"Adam": [2, 1], "Branda": 3, "David": [1, 4] ...}

with value from guests1 ieRorys_guests dict will take precedence first.ie for Adam key value is [2,1]来自 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 }

This should work:这应该有效:

def combine_guests(guests1, guests2):
    out = guests1.copy()
    for key, val in guests2.items():
        out[key] = val
    return out

EDIT: copied guests2 instead of guests1编辑:复制来宾2而不是来宾1

This might help.这可能会有所帮助。 This will give precedence to Rorys_guests and will not include those from Taylors_guests if both have the value.这将优先考虑Rorys_guests ,如果两者都有值,则不会包括来自Taylors_guests那些。

Taylors_guests.update(Rorys_guests)
return Rorys_guests

Try this it will work试试这个它会工作

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))

Open for any kind of suggestions打开任何类型的建议

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

output:输出:

{'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))

This is a bit late, as the OP submitted their question some time ago, but I guess I will submit my answer:这有点晚了,因为 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} 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}

Kindly note the update() method ie dict1.update(dict2) will not work here as it will defeat the purpose of the output desired by replacing the contents.请注意update()方法,即dict1.update(dict2)在这里不起作用,因为它会破坏通过替换内容所需的 output 的目的。

Solution:解决方案:

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: 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): def combine_guests(guests1, guests2):

for key in guests2.keys():对于 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, "Nancy":1, "Robert":2, "Adam":1, "Samantha":3, "Chris":5} 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}

print(combine_guests(Rorys_guests, Taylors_guests))打印(组合客人(Rorys_guests,Taylors_guests))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何结合两个词典? 一个字典作为关键,另一个字典作为价值 - How to combine two dictionaries? One dict as key, another dict as value 从dict1更新dict2的值,但在dict2中的特定位置 - Update value of dict2 from dict1 but in a specific place in dict2 使用dict1的键和dict2的值(这是dict1中的键)从dict1和dict2嵌套的字典 - nested dictionary from dict1 and dict2 using keys from dict1 and values from dict2(which are keys in dict1) 将字典列表中的两个列表合并为一个 - combine two lists in a list of dictionaries into one 您如何将两个相似的嵌套字典合而为一,每个字典都有一些共享且唯一的嵌套元素(Python)? - How do you combine two similar nested dictionaries into one, each with some shared and unique nested elements (Python)? 将dict1的值插入dict2,但在dict2中的特定位置 - Insert values of dict1 into dict2 but in a specific place in dict2 Python:如何验证 dict1 只是 dict2 的一个子集? 值都是 int 并且在 scope 内 - Python: How can one verify that dict1 is only a subset of dict2? Values are all int and within scope 将两个字典以不同的缩进级别合并为一个 - Combine two dict into one at different levels of indent 来自两个字典的元素进入列表 - Elements from two dictionaries into list 使用dict1中的变量存储键作为dict2的新键 - Use variable storing key from dict1 as new key for dict2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM