Filtering a Python list to uniques

Discussion in 'Python' started by kellygreer1, Mar 25, 2008.

  1. kellygreer1

    kellygreer1 Guest

    What is the best way to filter a Python list to its unique members?
    I tried some method using Set but got some "unhashable" error.

    lstone = [ 1, 2, 3, 3, 4, 5, 5, 6 ]
    # how do i reduce this to
    lsttwo = [ 1, 2, 3, 4, 5, 6 ]

    Is there a page on this in the Python in a Nutshell or the Python
    Cookbook?
    Did I miss something?

    Kelly Greer

    change nospam to yahoo
     
    kellygreer1, Mar 25, 2008
    #1
    1. Advertising

  2. kellygreer1

    Guest

    , Mar 25, 2008
    #2
    1. Advertising

  3. kellygreer1

    Mensanator Guest

    On Mar 25, 6:30 pm, kellygreer1 <> wrote:
    > What is the best way to filter a Python list to its unique members?
    > I tried some method using Set but got some "unhashable" error.
    >
    > lstone = [ 1, 2, 3, 3, 4, 5, 5, 6 ]
    > # how do i reduce this to
    > lsttwo = [ 1, 2, 3, 4, 5, 6 ]
    >
    > Is there a page on this in the Python in a Nutshell or the Python
    > Cookbook?
    > Did I miss something?


    I don't know, the set() soution worked for me.

    >>> lstone = [1,2,3,3,4,5,5,6]
    >>> setone = set(lstone)
    >>> lsttwo = list(setone)
    >>> lstone

    [1, 2, 3, 3, 4, 5, 5, 6]
    >>> setone

    set([1, 2, 3, 4, 5, 6])
    >>> lsttwo

    [1, 2, 3, 4, 5, 6]


    >
    > Kelly Greer
    >
    > change nospam to yahoo
     
    Mensanator, Mar 25, 2008
    #3
  4. On Mar 25, 4:30 pm, kellygreer1 <> wrote:
    > What is the best way to filter a Python list to its unique members?
    > I tried some method using Set but got some "unhashable" error.
    >
    > lstone = [ 1, 2, 3, 3, 4, 5, 5, 6 ]
    > # how do i reduce this to
    > lsttwo = [ 1, 2, 3, 4, 5, 6 ]


    If the elements are hashable, try this:
    lsttwo = sorted(set(lstone))
    If not hashable, try:
    lsttwo = [k for k,v in itertools.groupby(sorted(lstone))]


    Raymond
     
    Raymond Hettinger, Mar 26, 2008
    #4
  5. kellygreer1

    hellt Guest

    On 26 ÍÁÒ, 02:30, kellygreer1 <> wrote:
    > What is the best way to filter a Python list to its unique members?
    > I tried some method using Set but got some "unhashable" error.
    >
    > lstone = [ 1, 2, 3, 3, 4, 5, 5, 6 ]
    > # how do i reduce this to
    > lsttwo = [ 1, 2, 3, 4, 5, 6 ]
    >
    > Is there a page on this in the Python in a Nutshell or the Python
    > Cookbook?
    > Did I miss something?
    >
    > Kelly Greer
    >
    > change nospam to yahoo



    or just look this thread for a fastest solution
    http://groups.google.com/group/comp...d91ccea?lnk=gst&q=duplicates#af8961f1ed91ccea
     
    hellt, Mar 26, 2008
    #5
  6. kellygreer1

    kellygreer1 Guest

    On Mar 26, 5:45 am, hellt <> wrote:
    > On 26 ÍÁÒ, 02:30,kellygreer1<> wrote:
    >
    > > What is the best way to filter a Python list to its unique members?
    > > I tried some method using Set but got some "unhashable" error.

    >
    > > lstone = [ 1, 2, 3, 3, 4, 5, 5, 6 ]
    > > # how do i reduce this to
    > > lsttwo = [ 1, 2, 3, 4, 5, 6 ]

    >
    > > Is there a page on this in the Python in a Nutshell or the Python
    > > Cookbook?
    > > Did I miss something?

    >
    > > Kelly Greer
    > >
    > > change nospam to yahoo

    >
    > or just look this thread for a fastest solutionhttp://groups.google.com/group/comp.lang.python/browse_frm/thread/709...


    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?

    Thanks,
    Kelly
     
    kellygreer1, Mar 26, 2008
    #6
  7. kellygreer1

    Jerry Hill Guest

    On Wed, Mar 26, 2008 at 2:50 PM, kellygreer1 <> 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
     
    Jerry Hill, Mar 26, 2008
    #7
  8. En Wed, 26 Mar 2008 15:50:30 -0300, kellygreer1 <>
    escribió:

    > On Mar 26, 5:45 am, hellt <> wrote:
    >> On 26 ÍÁÒ, 02:30,kellygreer1<> wrote:
    >>
    >> > What is the best way to filter a Python list to its unique members?


    > 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?


    Mutable objects can't be used as keys in a dict nor be members of a set
    (at least if the mutable part is used to implement the == comparison).
    Technically, they should not implement __hash__ so hash(x) fails on them -
    they are "unhashable".

    Objects that can be used as keys in a dictionary or be members of a set
    include:
    instances of all numeric types, strings, tuples, frozensets; all of them
    are immutable and hashable. Instances of user-defined classes that don't
    implement __eq__ nor __cmp__ nor __hash__ are hashable and can be used
    too. Classes that implement such special methods must ensure that (x==y)
    => (hash(x)==hash(y)) and in that case can be used too; else, they should
    not implement __hash__ at all.

    On the other hand, mutable containers can't be keys in a dict, nor be
    members of a set; including instances of lists, sets, dicts, all of them
    are mutable and unhashable objects.

    Read the Cookbook recipes that someone posted before; one of them is
    generic enough to handle all cases, trying the fastest approaches first.

    If you think that all your objects should be hashable, try to find which
    one isn't.

    --
    Gabriel Genellina
     
    Gabriel Genellina, Mar 26, 2008
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. William Park
    Replies:
    0
    Views:
    588
    William Park
    Aug 2, 2004
  2. Harry Haller
    Replies:
    3
    Views:
    1,209
    RobinS
    Feb 1, 2007
  3. Phil

    Filtering List Of Css Styles Available

    Phil, Oct 11, 2007, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    713
  4. Adriano Lul

    Count uniques ?

    Adriano Lul, Apr 28, 2008, in forum: Ruby
    Replies:
    1
    Views:
    111
    Roger Pack
    Apr 28, 2008
  5. Jack
    Replies:
    22
    Views:
    265
    Ted Zlatanov
    Aug 10, 2006
Loading...

Share This Page