Yashvardhan Nanavati Yashvardhan Nanavati - 1 month ago 10
Python Question

unhashable type 'list' error

I am using the following code to count the number of occurrences of a particular number in a numpy array, sort the dictionary in a descending order and then return it

km_0 = [indian,chinese,italian,mexican,indian,indian,chinese,italian] #numpy array
#The ord_dict should be like this {indian:3, chinese:2, italian:2, mexican:1}

def labels(cluster):
label_count ={}
for i in cluster[0]:
if i in label_count:
label_count[i] += 1
else:
label_count[i] =1
ord_dict = OrderedDict(sorted(label_count.items(), key=lambda kv:kv[1], reverse=True))

return ord_dict


function call

lc = labels(km_0)


However, it throws up the following error

<ipython-input-8-72f0a128bdd4> in labels(cluster)
9 label_count ={}
10 for i in cluster[0]:
---> 11 if i in label_count:
12 label_count[i] += 1
13 else:

TypeError: unhashable type: 'list'

Answer

Maybe instead of building your own counter you could employ from collection Counter:

from collections import Counter, OrderedDict
x = "hello world"
print(OrderedDict(sorted(Counter(x).items(), key=lambda t: t[1], reverse=True))) 
#prints OrderedDict([('l', 3), ('o', 2), (' ', 1), ('e', 1), ('d', 1), ('h', 1), ('r', 1), ('w', 1)])

I still don't know what j is for yours I'm guessing it's a typo for i

Edited:

Above works for normal Arrays but for numpy use the follow using numpy's unique() function call :

#replace array_name with like your `i`
unique, counts = numpy.unique(array_name, return_counts=True)

#Then zip them together to make a dictionary

counted = dict(zip(unique, counts))

#then toss it into OrderedDict

print(OrderedDict(sorted(counted.items(), key=lambda t: t[1], reverse=True))) 

For more information on numpy.unique see here.