keys in dictionary

Discussion in 'Python' started by Shi Mu, Nov 22, 2005.

  1. Shi Mu

    Shi Mu Guest

    I run the following code and got wrong message, but I still want to
    make [1,2],[4,3] and [6,9]
    to be keys of the dictionary or change the style a little bit. How to do that?
    Thanks!

    >>> p=[[1,2],[4,3],[6,9]]
    >>> n=dict([(x,[]) for x in p])

    Traceback (most recent call last):
    File "<interactive input>", line 1, in ?
    TypeError: list objects are unhashable
    >>>
     
    Shi Mu, Nov 22, 2005
    #1
    1. Advertising

  2. Shi Mu wrote:
    > I run the following code and got wrong message, but I still want to
    > make [1,2],[4,3] and [6,9] to be keys of the dictionary or change the
    > style a little bit. How to do that?


    Make them tuples:

    >>> p=[[1,2],[4,3],[6,9]]
    >>> n=dict([(tuple(x),[]) for x in p])
    >>> n

    {(6, 9): [], (1, 2): [], (4, 3): []}

    Regards,
    Martin

    P.S. Please don't cross-post.
     
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=, Nov 22, 2005
    #2
    1. Advertising

  3. use (1,2) , (3,4)....


    "Shi Mu" <> a écrit dans le message de news:
    ...
    I run the following code and got wrong message, but I still want to
    make [1,2],[4,3] and [6,9]
    to be keys of the dictionary or change the style a little bit. How to do
    that?
    Thanks!

    >>> p=[[1,2],[4,3],[6,9]]
    >>> n=dict([(x,[]) for x in p])

    Traceback (most recent call last):
    File "<interactive input>", line 1, in ?
    TypeError: list objects are unhashable
    >>>
     
    Georges Barthelemy, Nov 22, 2005
    #3
    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. sandeep Kanwal

    serial keys/validation keys

    sandeep Kanwal, Oct 29, 2004, in forum: C++
    Replies:
    1
    Views:
    593
    Mike Wahler
    Oct 29, 2004
  2. Harry George
    Replies:
    9
    Views:
    721
    sonal
    Jun 13, 2006
  3. Replies:
    10
    Views:
    736
    Daniel T.
    Feb 3, 2006
  4. alan
    Replies:
    3
    Views:
    374
    Victor Bazarov
    Nov 28, 2007
  5. A. Farber
    Replies:
    10
    Views:
    244
    A. Farber
    Jun 12, 2004
Loading...

Share This Page