Getting values from keys and Viceversa

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • psbasha
    Contributor
    • Feb 2007
    • 440

    #1

    Getting values from keys and Viceversa

    Hi,

    Dictionary works in getting values from the key values.But I would like to get keys from values.

    For example :

    d = {100: [1,0,0],102: [0,1,0],202: [0,1,1]}

    d.get(102)

    o/p: [0,1,0]

    I need

    i/p

    [0,1,0]

    o/p : 102

    Thanks

    PSB
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by psbasha
    Hi,

    Dictionary works in getting values from the key values.But I would like to get keys from values.

    For example :

    d = {100: [1,0,0],102: [0,1,0],202: [0,1,1]}

    d.get(102)

    o/p: [0,1,0]

    I need

    i/p

    [0,1,0]

    o/p : 102

    Thanks

    PSB
    Keeping an inverse dictionary is not uncommon:
    Code:
    >>> d = {'a':1, 'b':2, 'c':3}
    >>> di = dict((v, k) for k, v in d.items())
    >>> di
    {1: 'a', 2: 'b', 3: 'c'}
    >>>

    Comment

    • psbasha
      Contributor
      • Feb 2007
      • 440

      #3
      Thanks for the reply.

      How to handle the dictionary when we have a Key as a number and Values are list?

      As I mentioned in my example

      -PSB

      Comment

      • psbasha
        Contributor
        • Feb 2007
        • 440

        #4
        Code:
        Sample>>> d = {100: [1,0,0],102: [0,1,0],202: [0,1,1]}
        >>> di = dict((v, k) for k, v in d.items())
        
        Traceback (most recent call last):
          File "<pyshell#9>", line 1, in -toplevel-
            di = dict((v, k) for k, v in d.items())
        TypeError: list objects are unhashable
        >>>
        I am getting the above error when I applied for the example I mentioned

        -PSB

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          Originally posted by psbasha
          Code:
          Sample>>> d = {100: [1,0,0],102: [0,1,0],202: [0,1,1]}
          >>> di = dict((v, k) for k, v in d.items())
          
          Traceback (most recent call last):
            File "<pyshell#9>", line 1, in -toplevel-
              di = dict((v, k) for k, v in d.items())
          TypeError: list objects are unhashable
          >>>
          I am getting the above error when I applied for the example I mentioned

          -PSB
          Try this:
          Code:
          >>> dict(zip([str(x) for x in d.values()], d.keys()))
          {'[0, 1, 1]': 202, '[1, 0, 0]': 100, '[0, 1, 0]': 102}
          >>>
          Lists are not hashable but strings are.

          Comment

          • ghostdog74
            Recognized Expert Contributor
            • Apr 2006
            • 511

            #6
            Originally posted by psbasha
            Hi,

            Dictionary works in getting values from the key values.But I would like to get keys from values.

            For example :

            d = {100: [1,0,0],102: [0,1,0],202: [0,1,1]}

            d.get(102)

            o/p: [0,1,0]

            I need

            i/p

            [0,1,0]

            o/p : 102

            Thanks

            PSB
            usually, the way to get values is through the values() method.
            for this case, you can use the dictionary values() method.
            eg
            Code:
            if  [0,1,0] in d.values():
                print "found "

            Comment

            • psbasha
              Contributor
              • Feb 2007
              • 440

              #7
              Originally posted by bvdet
              Try this:
              Code:
              >>> dict(zip([str(x) for x in d.values()], d.keys()))
              {'[0, 1, 1]': 202, '[1, 0, 0]': 100, '[0, 1, 0]': 102}
              >>>
              Lists are not hashable but strings are.
              So we have to create the list as string and use it for inversing and use it.

              Thanks in advance
              PSB

              Comment

              • bartonc
                Recognized Expert Expert
                • Sep 2006
                • 6478

                #8
                Originally posted by psbasha
                So we have to create the list as string and use it for inversing and use it.

                Thanks in advance
                PSB
                Not if you use tuples instead of lists.
                Lists let you assign into them (they are mutable) as in
                Code:
                aList[index] = valuse
                Tuples do not all item assignment. As long as you assign x, y, z all at the same time, as in
                Code:
                pt = x, y, z
                you can use tuples.
                Code:
                >>> d = {'a':(1,2,3),'b':(4,5,6),'c':(7,8,9)}
                >>> di = dict((v, k) for k, v in d.items())
                >>> di
                {(4, 5, 6): 'b', (7, 8, 9): 'c', (1, 2, 3): 'a'}
                >>>

                Comment

                • bvdet
                  Recognized Expert Specialist
                  • Oct 2006
                  • 2851

                  #9
                  Originally posted by bartonc
                  Not if you use tuples instead of lists.
                  Lists let you assign into them (they are mutable) as in
                  Code:
                  aList[index] = valuse
                  Tuples do not all item assignment. As long as you assign x, y, z all at the same time, as in
                  Code:
                  pt = x, y, z
                  you can use tuples.
                  Code:
                  >>> d = {'a':(1,2,3),'b':(4,5,6),'c':(7,8,9)}
                  >>> di = dict((v, k) for k, v in d.items())
                  >>> di
                  {(4, 5, 6): 'b', (7, 8, 9): 'c', (1, 2, 3): 'a'}
                  >>>
                  Good work Barton. I didn't think of tuples.
                  Code:
                  >>> d = {100: [1,0,0],102: [0,1,0],202: [0,1,1]}
                  >>> dict(zip([tuple(x) for x in d.values()], d.keys()))
                  {(0, 1, 0): 102, (0, 1, 1): 202, (1, 0, 0): 100}
                  >>>

                  Comment

                  • bartonc
                    Recognized Expert Expert
                    • Sep 2006
                    • 6478

                    #10
                    Originally posted by bvdet
                    Good work Barton. I didn't think of tuples.
                    Code:
                    >>> d = {100: [1,0,0],102: [0,1,0],202: [0,1,1]}
                    >>> dict(zip([tuple(x) for x in d.values()], d.keys()))
                    {(0, 1, 0): 102, (0, 1, 1): 202, (1, 0, 0): 100}
                    >>>
                    Take values() and keys() from a dictionary separately scares me a little. But I suppose that there's no reason for the dict to be re-orderd until a new item is add or an old one removed...

                    Comment

                    • bvdet
                      Recognized Expert Specialist
                      • Oct 2006
                      • 2851

                      #11
                      Originally posted by bartonc
                      Take values() and keys() from a dictionary separately scares me a little. But I suppose that there's no reason for the dict to be re-orderd until a new item is add or an old one removed...
                      That never bothered me until you mentioned it. Thinking about it - both d.keys() and d.values() are lists (ordered) so there should be no reason for the 'items' to misalign.

                      Comment

                      Working...