Just a quick one

Benjamin Niemann b.niemann at betternet.de
Wed Aug 25 09:19:29 EDT 2004


lists cannot be used as dictionary keys (because lists are mutable, they 
have no invariant hash value -> unhashable), use tuples instead:

nextName = {('Bob','One'):['Rita','Sue'],\
               'Rita':['Mary','Sue',['Bob','One']],\
               'Sue':['Rita','Mary',['Bob','One']],\
               'Mary':['Sue','Rita']}

M. Clift wrote:
> Hi,
> 
> Just a quick one. I'm trying to call [Bob','One'] with this, but I keep
> getting 'unhashable'. I've tried various ' " and [ and can't get the thing
> to work. Any offers?
> 
> Thanks,
> 
> M
> 
> <code>
> 
> from random import *
> 
> Name_Number = xrange(int(raw_input("Choose number of Names (1-20)? ")))
> 
> state = [None,None]
> 
> nextName = {['Bob','One']:['Rita','Sue'],\
>               'Rita':['Mary','Sue',['Bob','One']],\
>               'Sue':['Rita','Mary',['Bob','One']],\
>               'Mary':['Sue','Rita']}
> 
> Name_List = []
> 
> tmp = choice((['Bob','One'],'Rita','Sue','Mary'))
> 
> for x in Name_Number:
>     print state
>     while tmp in state[0:2]:
>         tmp = choice(nextName[Name_List[-1]])
>     print tmp, ", ",
>     print
>     print "Name ",x+1," is ", tmp
>     Name_List.append(tmp)
>     state[x%2] = tmp
> 
>     print Name_List
> 
> <code>
> 
> 



More information about the Python-list mailing list