Filtering a Python list to uniques
Jerry Hill
malaclypse2 at gmail.com
Wed Mar 26 15:43:18 EDT 2008
On Wed, Mar 26, 2008 at 2:50 PM, kellygreer1 <kellygreer1 at yahoo.com> wrote:
> How come the Set() thing seems to work for some people and I get the
> 'unhashable' error?
>
> How do you test for 'membership' on a dictionary?
>
> # where tmp is the non-unique list
> # dct is a dictionary where each unique key will be tied to a count
> (the value)
> # for testing I was setting the count to 0
> for v in tmp:
> if not v in dct: dct[v] = 0
>
> # I get unhashable error here.
> # Even if I write it.
>
> for v in tmp:
> if not v in dct.keys(): dct[v] = 0
>
> What am I missing?
Some of the elements of tmp are unhashable. Unhashable items can't be
the keys of a dictionary or members of a set. I don't think you've
said anywhere in the thread what these items are, you just started out
with an example of a list of integers. Do you believe the elements in
tmp are integers? If so, try the following -
for v in tmp:
print type(v), repr(v), hash(v)
and let us know what it spits out.
--
Jerry
More information about the Python-list
mailing list