Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > keys in dictionary

Reply
Thread Tools

keys in dictionary

 
 
Shi Mu
Guest
Posts: n/a
 
      11-22-2005
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
>>>

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
Guest
Posts: n/a
 
      11-22-2005
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.
 
Reply With Quote
 
 
 
 
Georges Barthelemy
Guest
Posts: n/a
 
      11-22-2005
use (1,2) , (3,4)....


"Shi Mu" <(E-Mail Removed)> a écrit dans le message de news:
http://www.velocityreviews.com/forums/(E-Mail Removed)...
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
>>>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Performance ordered dictionary vs normal dictionary Navkirat Singh Python 6 07-29-2010 10:18 AM
Re: Performance ordered dictionary vs normal dictionary Chris Rebert Python 0 07-29-2010 06:11 AM
creating a dictionary from a dictionary with regex james_027 Python 1 08-22-2007 07:39 AM
[DICTIONARY] - Copy dictionary entries to attributes Ilias Lazaridis Python 6 02-21-2006 11:27 AM
dictionary within dictionary Fox ASP General 5 03-13-2005 05:03 AM



Advertisments