join dictionaries using keys from one & values

Discussion in 'Python' started by ProvoWallis, Dec 6, 2005.

  1. ProvoWallis

    ProvoWallis Guest

    I'm still learning python so this might be a crazy question but I
    thought I would ask anyway. Can anyone tell me if it is possible to
    join two dictionaries together to create a new dictionary using the
    keys from the old dictionaries?

    The keys in the new dictionary would be the keys from the old
    dictionary one (dict1) and the values in the new dictionary would be
    the keys from the old dictionary two (dict2). The keys would be joined
    by matching the values from dict1 and dict2. The keys in each
    dictionary are unique.

    dict1 = {1:'bbb', 2:'aaa', 3:'ccc'}

    dict2 = {5.01:'bbb', 6.01:'ccc', 7.01:'aaa'}

    dict3 = {1 : 5.01, 3 : 6.01, 2 : 7.01}

    I looked at "update" but I don't think it's what I'm looking for.

    Thanks,

    Greg
     
    ProvoWallis, Dec 6, 2005
    #1
    1. Advertising

  2. ProvoWallis wrote:

    > I'm still learning python so this might be a crazy question but I
    > thought I would ask anyway. Can anyone tell me if it is possible to
    > join two dictionaries together to create a new dictionary using the
    > keys from the old dictionaries?


    There is no builtin method. The usual way is to just wrap a class
    around two dictionaries, one for mapping keys to values and the other
    for mapping values back to keys.

    --
    Erik Max Francis && && http://www.alcyone.com/max/
    San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
    Yes I'm / Learning from falling / Hard lessons
    -- Lamya
     
    Erik Max Francis, Dec 6, 2005
    #2
    1. Advertising

  3. ProvoWallis

    Guest

    ProvoWallis wrote:
    > I'm still learning python so this might be a crazy question but I
    > thought I would ask anyway. Can anyone tell me if it is possible to
    > join two dictionaries together to create a new dictionary using the
    > keys from the old dictionaries?
    >
    > The keys in the new dictionary would be the keys from the old
    > dictionary one (dict1) and the values in the new dictionary would be
    > the keys from the old dictionary two (dict2). The keys would be joined
    > by matching the values from dict1 and dict2. The keys in each
    > dictionary are unique.
    >
    > dict1 = {1:'bbb', 2:'aaa', 3:'ccc'}
    >
    > dict2 = {5.01:'bbb', 6.01:'ccc', 7.01:'aaa'}
    >
    > dict3 = {1 : 5.01, 3 : 6.01, 2 : 7.01}
    >
    > I looked at "update" but I don't think it's what I'm looking for.
    >
    > Thanks,

    If you can be sure that the value is hashable, I think you can just
    invert one of the dict(key/value flipped) and a for loop to create the
    new dict

    dict2x = dict( ((dict2[k], k) for k in dict2.iterkeys()))
    dict3 = dict(((k, dict2x[v]) for k,v in dict1.iteritems()))

    This doesn't handle the case where v is in dict1 but not in dict2, it
    can be filtered out though.
     
    , Dec 6, 2005
    #3
  4. ProvoWallis

    ProvoWallis Guest

    Thanks so much. I never would have been able to figure this out on my
    own.

    def dictionary_join(one, two):

    dict2x = dict( ((dict2[k], k) for k in dict2.iterkeys()))
    dict3 = dict(((k, dict2x[v]) for k,v in dict1.iteritems()))
    print dict3

    dict1 = {1:'bbb', 2:'aaa', 3:'ccc'}

    dict2 = {'5.01':'bbb', '6.01':'ccc', '7.01':'aaa'}

    dictionary_join(dict1, dict2)
     
    ProvoWallis, Dec 6, 2005
    #4
  5. ProvoWallis

    Devan L Guest

    ProvoWallis wrote:
    > Thanks so much. I never would have been able to figure this out on my
    > own.
    >
    > def dictionary_join(one, two):
    >
    > dict2x = dict( ((dict2[k], k) for k in dict2.iterkeys()))
    > dict3 = dict(((k, dict2x[v]) for k,v in dict1.iteritems()))
    > print dict3
    >
    > dict1 = {1:'bbb', 2:'aaa', 3:'ccc'}
    >
    > dict2 = {'5.01':'bbb', '6.01':'ccc', '7.01':'aaa'}
    >
    > dictionary_join(dict1, dict2)


    You might want to make a working function.

    def join_dicts(d1,d2):
    temp = dict(((d2[k], k) for k in d2.iterkeys()))
    joined = dict(((k, temp[v]) for k,v in d1.iteritems()))
    return joined
     
    Devan L, Dec 6, 2005
    #5
  6. ProvoWallis <> wrote:
    ...
    > The keys in the new dictionary would be the keys from the old
    > dictionary one (dict1) and the values in the new dictionary would be
    > the keys from the old dictionary two (dict2). The keys would be joined
    > by matching the values from dict1 and dict2. The keys in each
    > dictionary are unique.


    ....but are the VALUES unique...? That's the crucial issue and you don't
    mention anything about it.

    > dict1 = {1:'bbb', 2:'aaa', 3:'ccc'}
    >
    > dict2 = {5.01:'bbb', 6.01:'ccc', 7.01:'aaa'}
    >
    > dict3 = {1 : 5.01, 3 : 6.01, 2 : 7.01}


    But what if in dict1 both keys 2 and 3 had a corresponding value of
    'ccc' -- what would you want as a result then? What if key 1 had a
    corresponding value of 'ddd' -- not a value in dict2; what would you
    want THEN? Without a more complete specification, it's impossible to
    tell, and one key Python principle is "in the face of ambiguity, refuse
    the temptation to guess".

    If values are assured to be unique, and the sets of values of the two
    dictionaries are assured to be identical, then the suggestion (already
    given in another post) to invert dict2 is a good idea, i.e., as a
    function:

    def PWmerge(d1, d2):
    invd = dict((v2, k2) for k2, v2 in d2.iteritems())
    return dict((k1,invd[v1]) for k1,v1 in d1.iteritems())

    but without all of the above assurances, different tweaks may be needed
    depending on what exactly you want to happen in the several "anomalous"
    cases.


    Alex
     
    Alex Martelli, Dec 6, 2005
    #6
  7. ProvoWallis

    Guest

    Super simple:

    dict3 = {}
    for k1 in dict1.keys():
    for k2 in dict2.keys():
    if dict1.get(k1) == dict2[k2]:
    dict3[k1] = k2

    works in all cases and can be simplified to an iterated dictionary in
    python 2.4
     
    , Dec 6, 2005
    #7
  8. ProvoWallis

    ProvoWallis Guest

    Thanks again. This is very helpful.
     
    ProvoWallis, Dec 7, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alan Silver
    Replies:
    0
    Views:
    914
    Alan Silver
    Jun 5, 2006
  2. googleboy
    Replies:
    1
    Views:
    953
    Benji York
    Oct 1, 2005
  3. Tim Chase
    Replies:
    6
    Views:
    300
    Michael J. Fromberger
    Feb 28, 2006
  4. lysdexia
    Replies:
    6
    Views:
    560
    John Machin
    Dec 2, 2007
  5. Brandon
    Replies:
    12
    Views:
    516
    Brandon
    Aug 15, 2008
Loading...

Share This Page