[Tutor] how to understand unhashable type: 'list'
Christian Witts
cwitts at compuscan.co.za
Thu Nov 17 15:12:45 CET 2011
On 2011/11/17 03:56 PM, lina wrote:
> <snip>
>> _______________________________________________
>> Tutor maillist - Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>>
>> sum(1 if type(elem) == list else 0 for elem in list1) not work for you if
>> all you want to do is count how many lists you have in your main list ?
>>
>> not count how many sublists in the main list.
>>
>> wanna count the occurence of the sublists,
>>
>> I mean, something like
>>
>> dictionary[list1[1]]=occurence
>>
>> Traceback (most recent call last):
>> File "<pyshell#298>", line 1, in<module>
>> dictionary[list1[1]]=1
>> TypeError: unhashable type: 'list'
>>
>> dictionary[list1[1]]=1
>>
>> Traceback (most recent call last):
>> File "<pyshell#298>", line 1, in<module>
>> dictionary[list1[1]]=1
>> TypeError: unhashable type: 'list'
>>
>>
>> For that you'll need to convert your list to a hash-able type if you want to
>> use dictionaries for your store, easiest would probably to use the string
>> representation for it so you could do
>>
>>>>> list1 = [['61', '34', '61', '34'], ['61', '35', '61', '70', '61'],
>>>>> ['61', '70', '61', '34'], ['34', '58', '34', '58']]
>>>>> weight = {}
>>>>> for elem in list1:
>> ... if elem.__repr__() in weight:
> This is cool.
>
> May I ask which role the __repr__ plays here?
>
>
__repr__ is the string representation of a Python object [1] so I'm
using it to create something that is hash-able to be used as the
dictionary key value.
[1] http://docs.python.org/library/functions.html#repr
--
Christian Witts
Python Developer
//
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111117/ae76b828/attachment.html>
More information about the Tutor
mailing list