How to do an 'inner join' with dictionaries

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cyborg4@walla.co.il

    How to do an 'inner join' with dictionaries

    Let's say I have two dictionaries:
    dict1 is 1:23, 2:76, 4:56
    dict2 is 23:A, 76:B, 56:C

    How do I get a dictionary that is
    1:A, 2:B, 4:C

  • Xavier Morel

    #2
    Re: How to do an 'inner join' with dictionaries

    cyborg4@walla.c o.il wrote:[color=blue]
    > Let's say I have two dictionaries:
    > dict1 is 1:23, 2:76, 4:56
    > dict2 is 23:A, 76:B, 56:C
    >
    > How do I get a dictionary that is
    > 1:A, 2:B, 4:C
    >[color=green][color=darkred]
    >>> dict1 = {1:23,2:76,4:56 }
    >>> dict2 = {23:'A',76:'B', 56:'C'}
    >>> dict((k, dict2[v]) for k, v in dict1.items())[/color][/color][/color]
    {1: 'A', 2: 'B', 4: 'C'}[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    ?

    Comment

    • Kent Johnson

      #3
      Re: How to do an 'inner join' with dictionaries

      cyborg4@walla.c o.il wrote:[color=blue]
      > Let's say I have two dictionaries:
      > dict1 is 1:23, 2:76, 4:56
      > dict2 is 23:A, 76:B, 56:C
      >
      > How do I get a dictionary that is
      > 1:A, 2:B, 4:C
      >[color=green][color=darkred]
      >>> dict1 = {1:23, 2:76, 4:56}
      >>> dict2 = {23:'A', 76:'B', 56:'C'}
      >>> dict((key, dict2[value]) for key, value in dict1.iteritems ())[/color][/color][/color]
      {1: 'A', 2: 'B', 4: 'C'}

      Kent

      Comment

      • Tim Chase

        #4
        Re: How to do an 'inner join' with dictionaries

        > Let's say I have two dictionaries:[color=blue]
        > dict1 is 1:23, 2:76, 4:56
        > dict2 is 23:A, 76:B, 56:C
        >
        > How do I get a dictionary that is
        > 1:A, 2:B, 4:C[/color]
        [color=blue][color=green][color=darkred]
        >>> d1={1:23,2:76,4 :56}
        >>> d2={23:"a", 76:"b", 56:"c"}
        >>> result = dict([(d1k,d2[d1v]) for (d1k, d1v) in d1.items()])
        >>> result[/color][/color][/color]
        {1: 'a', 2: 'b', 4: 'c'}



        If you don't know that all the values in d1 will be in d2,
        [color=blue][color=green][color=darkred]
        >>> d1[5]=444
        >>> d2[333]='qqq'[/color][/color][/color]

        you can put in a "if" check like
        [color=blue][color=green][color=darkred]
        >>> result = dict([(d1k,d2[d1v]) for (d1k, d1v) in[/color][/color][/color]
        d1.items() if d1v in d2])
        {1: 'a', 2: 'b', 4: 'c'}

        (using "d1v" for "dictionary 1's value" and "d1k" for
        "dictionary 1's key")

        HTH,

        -tkc




        Comment

        • Antoon Pardon

          #5
          Re: How to do an 'inner join' with dictionaries

          Op 2006-02-27, cyborg4@walla.c o.il schreef <cyborg4@walla. co.il>:[color=blue]
          > Let's say I have two dictionaries:
          > dict1 is 1:23, 2:76, 4:56
          > dict2 is 23:A, 76:B, 56:C
          >
          > How do I get a dictionary that is
          > 1:A, 2:B, 4:C[/color]

          Well how about the straight forward:

          dict3 = {}
          for key, value in dict1.iteritems ():
          try:
          dict3[key] = dict2[value]
          except KeyError:
          pass

          Or otherwise with a genexp

          dict3 = dict((key, dict2[value]) for (key, value) in dict1.iteritems () if value in dict2)

          --
          Antoon Pardon

          Comment

          • Claudio Grondi

            #6
            Re: How to do an 'inner join' with dictionaries

            cyborg4@walla.c o.il wrote:[color=blue]
            > Let's say I have two dictionaries:
            > dict1 is 1:23, 2:76, 4:56
            > dict2 is 23:A, 76:B, 56:C
            >
            > How do I get a dictionary that is
            > 1:A, 2:B, 4:C
            >[/color]
            Just copy/paste the following source code to a file and run it:
            <code>
            sourceCodeToExe cute = """
            dict1 = { 1:23, 2:76, 4:56 }
            dict2 = { 23:'A', 76:'B', 56:'C' }
            # How do I get a dictionary that is
            dict3 = { 1:'A', 2:'B', 4:'C' }

            dict4 = {}
            for key in dict1.keys():
            dict4[key] = dict2[dict1[key]]
            #:for

            print 'dict3 == dict4 is', dict3 == dict4
            """

            print
            print ' execution of following source code: '
            print
            print '"""',
            print sourceCodeToExe cute
            print '"""'
            print
            print ' results in output of: '
            print
            exec(sourceCode ToExecute)
            </code>

            The core idea of an inner join:
            dict2[dict1[key]]


            Hope this helps inspite of the fact there is some more complicated code
            involved, which I provided to show the beauty of Python from the point
            of view of my own style of writing 'tutorial-like' code which explains
            itself by its output.

            Claudio

            Comment

            • Michael J. Fromberger

              #7
              Re: How to do an 'inner join' with dictionaries

              In article <1141047580.492 771.97130@z34g2 000cwc.googlegr oups.com>,
              cyborg4@walla.c o.il wrote:
              [color=blue]
              > Let's say I have two dictionaries:
              > dict1 is 1:23, 2:76, 4:56
              > dict2 is 23:A, 76:B, 56:C
              >
              > How do I get a dictionary that is
              > 1:A, 2:B, 4:C[/color]

              The following should work:

              j = dict((k, dict2[dict1[k]]) for k in dict1 if dict1[k] in dict2)

              Cheers,
              -M

              --
              Michael J. Fromberger | Lecturer, Dept. of Computer Science
              http://www.dartmouth.edu/~sting/ | Dartmouth College, Hanover, NH, USA

              Comment

              Working...