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. Advertisements

  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. Advertisements

  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. Advertisements

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:
    739
    Mike Wahler
    Oct 29, 2004
  2. =?Utf-8?B?RnJhbms=?=

    ContainsKey with Custom Dictionary Keys

    =?Utf-8?B?RnJhbms=?=, Jul 5, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    450
    =?Utf-8?B?RnJhbms=?=
    Jul 5, 2006
  3. Quentin Crain
    Replies:
    8
    Views:
    581
    Terry Reedy
    Oct 11, 2003
  4. =?ISO-8859-1?Q?Jan-Erik_Meyer-L=FCtgens?=

    dictionary keys, __hash__, __cmp__

    =?ISO-8859-1?Q?Jan-Erik_Meyer-L=FCtgens?=, Nov 4, 2003, in forum: Python
    Replies:
    8
    Views:
    557
    Michael Hudson
    Nov 5, 2003
  5. Harry George
    Replies:
    9
    Views:
    942
    sonal
    Jun 13, 2006
  6. Replies:
    10
    Views:
    952
    Daniel T.
    Feb 3, 2006
  7. alan
    Replies:
    3
    Views:
    594
    Victor Bazarov
    Nov 28, 2007
  8. A. Farber
    Replies:
    10
    Views:
    436
    A. Farber
    Jun 12, 2004
Loading...