Just a quick one

Jaco Smuts JSmuts at clover.co.za
Wed Aug 25 15:15:58 CEST 2004


Hello M

>From the python tutorial:
"Another useful data type built into Python is the dictionary. 
Dictionaries are sometimes found in other languages as ``associative 
memories'' or ``associative arrays''. Unlike sequences, which are indexed 
by a range of numbers, dictionaries are indexed by keys, which can be any 
immutable type; strings and numbers can always be keys. Tuples can be used 
as keys if they contain only strings, numbers, or tuples; if a tuple 
contains any mutable object either directly or indirectly, it cannot be 
used as a key. You can't use lists as keys, since lists can be modified in 
place using their append() and extend() methods, as well as slice and 
indexed assignments. "



I understand this to mean that your  code will not work becuase you are 
using a list (mutable) as a key

sounds as though it will work if you change 

['Bob','One']

to

('Bob', 'One')

I have not tested it, but I  suspect that will work

jaco








"M. Clift" <noone at here.com>
Sent by: python-list-bounces+jsmuts=clover.co.za at python.org
08/25/2004 03:06 PM
 
        To:     python-list at python.org
        cc: 
        Subject:        Just a quick one


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>


-- 
http://mail.python.org/mailman/listinfo/python-list

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20040825/168613fa/attachment.html>


More information about the Python-list mailing list