Python Forum
Union of dictionaries (taking max value on collision)
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Union of dictionaries (taking max value on collision)
#1
i need to write a program that takes two dictionaries and returns their union my problem is that if key is found in both, the larger value should be
used in the output.
example for dictionaries:
{'A':1,'B':5,'C':3},{'B':4, 'C':5, 'D':6})
Reply
#2
and what do you have so far?
Recommended Tutorials:
Reply
#3
(Jan-17-2018, 08:30 PM)metulburr Wrote: New Reply 
Top Page
Python Forum › Python Coding › General Coding Help
need help


im new in python so im not sure about it
dicA=[A:a,B:b,C:c,D:d]
dicB=[A:w,B:x,C:y,D:z]

dict_union=dicA.update(dicB)
return(dict_union)
Reply
#4
There is a syntax error at line 1. My python interpreter says
  File "updict.py", line 1
    dicA=[A:a,B:b,C:c,D:d]
           ^
SyntaxError: invalid syntax
Reply
#5
changed it to
dicA=['A':a,'B':b,'C':c,'D':d]
dicB=['A':w,'B':x,'C':y,'D':z]

dict_union=dicA.update(dicB)
print(dict_union)
Reply
#6
You need to actually try to run the code you are providing or this process isn't going to work.
  File "arghlebargle.py", line 1
    dicA=['A':a,'B':b,'C':c,'D':d]
             ^
SyntaxError: invalid syntax
Dictionaries are declared with {} not []
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Star I was making code for collision for my character izmamonke 2 2,054 Aug-06-2021, 04:30 PM
Last Post: izmamonke
  Inheritance vs Union gserranowong 3 2,206 Jun-29-2021, 06:13 PM
Last Post: gserranowong
  SOLVED - Collision detection - TURTLE OuateDePhoque 9 11,166 Nov-18-2020, 06:29 PM
Last Post: OuateDePhoque
  ImportError: cannot import name 'Union' from '_ctypes' (unknown location) ciuffoly 15 10,260 Oct-09-2020, 06:58 AM
Last Post: ciuffoly
  bouncing ball with variable collision points (in time) Zhaleh 1 2,326 Jul-24-2020, 02:54 PM
Last Post: Marbelous
  Lists union and intersecion arbiel 5 2,780 Mar-28-2020, 05:57 AM
Last Post: buran
  Player object wont recognize collision with other objects. Jan_97 3 2,659 Dec-22-2019, 04:08 PM
Last Post: joe_momma
  Collision function problem jtstewart95 2 2,719 May-02-2018, 11:44 PM
Last Post: scidam
  Need help figuring out Sprite.Group Collision code PySam 3 3,323 Sep-15-2017, 03:00 AM
Last Post: PySam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020