简体   繁体   中英

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

{"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]

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

This might help. This will give precedence to Rorys_guests and will not include those from Taylors_guests if both have the value.

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:

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}

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.

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:

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

for key in 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}

print(combine_guests(Rorys_guests, Taylors_guests))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM