Python Forum
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
merging two dictionaries
#11
Where 'two' gone?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#12
(Oct-03-2017, 10:27 AM)wavic Wrote: Where 'two' gone?
that is dict - no duplicate keys and that is the desired result - if there are duplicate keys, take the key:item pair from the second dict
Reply
#13
If someone wonder about **,so is it PEP 448 and work for 3.5-->.
wavic Wrote:Where 'two' gone?
The task here was not to retaining values,as mention dictionary can not have duplicate keys.
If want to retaining values when there is key collisions use defaultdict.
>>> from itertools import chain
... from collections import defaultdict
... d1 = {1:'one',2:'two'}
... d2 = {2:'deux',3:'trois'}
... d3 = defaultdict(list)
... for k, v in chain(d1.items(), d2.items()):
...     d3[k].append(v)

>>> d3
defaultdict(<class 'list'>, {1: ['one'], 2: ['two', 'deux'], 3: ['trois']})
Reply
#14
i created a thing in C in 2 layers called avlmap.  the lower layer was more like a mechanism to manage an AVL tree (caller provides comparison functions, worked with pointers, inserting nodes, etc).  the upper layer was more abstract, which is not much in C (caller worked with key:value pairs, inserting keys, assigning values, while the library managed a cursor that tracked key position).  the upper layer had a separate insert function for duplicate keys.  duplicates were actually real nodes in the AVL tree, other functions dealt with duplicates with no issues.  searching a key that had duplicates would get the first of them (the first in order of insertion).  doing a position move forward would get the next key.  doing a position move back would go back to the first key.  moving forward until the key changes and moving back one would be at the last of the duplicate keys.  without keys and with just values it worked like a list.  with just keys and without values it worked like a set.  an assembly language version of it for IBM mainframes was written a few years before (all my mainframe code was lost). you can get the C source as a tarball compressed by gzip, bzip2, or xz.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#15
nice!  i wish it was also in 2.7.

i went ahead and wrote dictmerge().
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#16
Just don't use legacy Python and/or versions which are older then 2 years. Since Python 3.5 (i guess) you can use key unpacking inside a dict constructor. Here an example:

d1 = {1:'one',2:'two'}
d2 = {2:'deux',3:'trois'}
d3 = {**d1, **d2}
print(d3)
Yay, it does what is looks like.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#17
(Oct-04-2017, 05:46 AM)Skaperen Wrote: nice!  i wish it was also in 2.7.

i went ahead and wrote dictmerge(). 

The very first line of that is #!/usr/bin/env python3. So if you're definitely always using python3, why do you import so many things from future, and do any version checking at all?
Reply
#18
(Oct-04-2017, 03:05 PM)nilamo Wrote:
(Oct-04-2017, 05:46 AM)Skaperen Wrote: nice!  i wish it was also in 2.7.

i went ahead and wrote dictmerge().     

The very first line of that is #!/usr/bin/env python3. So if you're definitely always using python3, why do you import so many things from future, and do any version checking at all?    
just because there is a #! doesn't mean it will always be used.  someone might do python whatever.py.  or they might import it (to have the function) into a script run by python2.

also, i use the same from __future__ import ... in all my .py files.  it's my pattern of file maintenance.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  merging three dictionaries Skaperen 3 1,933 Oct-20-2020, 10:06 PM
Last Post: Skaperen
  Merging Dictionaries - Optimum Style? adt 5 2,885 Oct-09-2019, 05:26 PM
Last Post: adt
  merging dictionaries Skaperen 3 2,438 Nov-13-2018, 06:26 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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