"Norman Khine" <[EMAIL PROTECTED]> wrote


but when I run this, I get the following error on the line:

if x and y and (x, y) in table:

TypeError: list objects are unhashable

Please post the entire error. The tiny bit you posted was
not overly helpful, usually the stacjk trace contains the
actual cause of the problem or a strong clue. However,
in this case...

x = ['airlines-scheduled', 'airport-car-parking']

You cannot use a list to access a dictionary because
dictionary keys must be immutable, and lists aren't.
When you use the 'in' operation on a dictionary it
basically searches the keys.

I am basically looping through files within each file, I have an entity, for example:

file1
<topic>airlines-scheduled airport-car-parking</topic>

file2
<topic>airport-car-parking</topic>

etc...

So the above code counts all occurrences of 'airport-car-parking' and sums it up.

All works well if, I change the code to:

        for brain in brains:
            x = getattr(brain, horizontal)
            x = string.join(x, '' )
            y = getattr(brain, vertical)
            y = string.join(y, '' )
            if x and y and (x, y) in table:
                table[(x, y)] += 1
                table[(x, '')] += 1
                table[('', y)] += 1
                table[('', '')] += 1

So now my list becomes a string, which is not really good for me, as this fails when there is more then one item.

Use a tuple instead of a list.
t = tuple(L)

You can use a tuple as an index to a dictionary.

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to