[Tutor] list objects are unhashable
Norman Khine
norman at khine.net
Mon Jun 30 22:41:02 CEST 2008
Hi,
Sorry, but this did not work.
I have done this, which returns the values I want (sort of)
for brain in brains:
x = getattr(brain, horizontal)
if isinstance(x, list):
for item in x:
x = item
else:
x
y = getattr(brain, vertical)
if isinstance(y, list):
for item in y:
y = item
else:
y
if x and y and (x, y) in table:
table[(x, y)] += 1
table[(x, '')] += 1
table[('', y)] += 1
table[('', '')] += 1
The only think is that, the totals are correct, but when there is an
item that is in both, it is counted only once.
Cédric Lucantis wrote:
> Le Monday 30 June 2008 21:31:35, vous avez écrit :
>> Thanks,
>> but where do i replace the x with tuple(x)
>>
>
> Whenever x is hashed, ie used as a key in a dictionary. You said you have:
>
>>>> table[(x, y)] += 1
>>>> where:
>>>>
>>>> x = ['airlines-scheduled', 'airport-car-parking']
>
> so you should rather write this (if y is a list too):
>
> table[(tuple(x), tuple(y))] += 1
>
> but of course you can also use tuples directly if your lists don't have to be
> modified:
>
> x = ('airlines-scheduled', 'airport-car-parking')
> y = (...)
> table[(x, y)] += 1
>
More information about the Tutor
mailing list