hi there!

i'm have a biiig trouble. I've make a simple chatbot, as can learning.
The problem is, that I can't insert data into the dictionary without a error.
here is the code:

codes = {}

def arraycompare(array1, array2):
    l = 0
    for i in array1:
        for j in array2:
            if i == j:
                l = l + 1
    return l

while True:
    try:
        obj = open("answers.txt", "r")
        codes = dict(obj.read())
        obj.close()
    except IOError:
        pass

    inpt = raw_input("<<<").split()
    fzylgc = {}
    for i in codes:
        fzylgc[i] = arraycompare(inpt, i)
    Bv = 0
    key = None

    for i in fzylgc:
        if fzylgc[i] > bv:
            key = i
            Bv = Bv + 1
    if Bv <= 1:
        inpt2 = raw_input("?...")
        codes[inpt] = inpt2
        obj = open("answers.txt", "w")
        obj.write(str(codes))
        obj.close()
    else:
        print codes[key]

when I'm running the program, this is ganna happend:

>>> 
<<<Hello
?...hi There!!
Traceback (most recent call last):
  File "C:\develop\module test\AI.py", line 32, in <module>
    codes[inpt] = inpt2
TypeError: unhashable type: 'list'
>>>

I hope that someones can help me and that I've explain enought, but the code is saying the most, right

Edited 6 Years Ago by emorjon2: n/a

Key in a dictionary can not use list.
If you turn it around is ok,and set list as an value.

>>> codes = {}
>>> inpt = raw_input("<<<").split()
<<<my car
>>> inpt
['my', 'car']
>>> inpt2 = raw_input("?...")
?...black
>>> inpt2
'black'
>>> codes[inpt] = inpt2

Traceback (most recent call last):
  File "<pyshell#16>", line 1, in <module>
    codes[inpt] = inpt2
TypeError: unhashable type: 'list'
>>> codes[inpt2] = inpt
>>> codes
{'black': ['my', 'car']}
>>>

Edited 6 Years Ago by snippsat: n/a

A key in a dictionary must be immutable (the error message does state that requirement, but so subtly that you did not notice: Mutable types should not be hashed), and lists are mutable. You can use your list as a key if you cast it to a tuple first.

This question has already been answered. Start a new discussion instead.