Filtering a Python list to uniques

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kellygreer1

    Filtering a Python list to uniques

    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
    kellygreer1@nos pam.com
    change nospam to yahoo
  • bearophileHUGS@lycos.com

    #2
    Re: Filtering a Python list to uniques

    Kelly Greer:
    What is the best way to filter a Python list to its unique members?
    If Python is "batteries included", then an industrial-strength
    unique() seems one of the most requested 'batteries' that's not
    included :-) I feel that it's coming in Python 2.6/3.x. In the
    meantime:



    Bye,
    bearophile

    Comment

    • Mensanator

      #3
      Re: Filtering a Python list to uniques

      On Mar 25, 6:30 pm, kellygreer1 <kellygre...@ya hoo.comwrote:
      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
      kellygre...@nos pam.com
      change nospam to yahoo

      Comment

      • Raymond Hettinger

        #4
        Re: Filtering a Python list to uniques

        On Mar 25, 4:30 pm, kellygreer1 <kellygre...@ya hoo.comwrote:
        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(lsto ne))
        If not hashable, try:
        lsttwo = [k for k,v in itertools.group by(sorted(lston e))]


        Raymond

        Comment

        • hellt

          #5
          Re: Filtering a Python list to uniques

          On 26 ÍÁÒ, 02:30, kellygreer1 <kellygre...@ya hoo.comwrote:
          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
          kellygre...@nos pam.com
          change nospam to yahoo

          or just look this thread for a fastest solution

          Comment

          • kellygreer1

            #6
            Re: Filtering a Python list to uniques

            On Mar 26, 5:45 am, hellt <Dodin.Ro...@gm ail.comwrote:
            On 26 ÍÁÒ, 02:30,kellygree r1<kellygre...@ yahoo.comwrote:
            >
            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
            kellygre...@nos pam.com
            change nospam to yahoo
            >
            or just look this thread for a fastest solutionhttp://groups.google.c om/group/comp.lang.pytho n/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

            Comment

            • Jerry Hill

              #7
              Re: Filtering a Python list to uniques

              On Wed, Mar 26, 2008 at 2:50 PM, kellygreer1 <kellygreer1@ya hoo.comwrote:
              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

              Comment

              Working...