Unique Elements in a List

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • superprad@gmail.com

    Unique Elements in a List

    Is there an easy way to grab the Unique elements from a list?
    For Example:
    data = [0.1,0.5,0.6,0.4 ,0.1,0.5,0.6,0. 9]

    what I am looking for is the unique elements 0.4 and 0.9 with their
    index from the list.
    Probably something like a Hash Table approach!!
    I would like to get this done without unnecessary overhead.And the list
    could be essentially anything strings,floats, int etc...

    Or is it already avaliable as an attr to a list or an array?
    I dont remember seeing anything like that.

  • superprad@gmail.com

    #2
    Re: Unique Elements in a List

    OK I need to be more clear I guess!Unique Elements I mean, elements
    that are non repeating. so in the above list 0.4, 0.9 are unique as
    they exist only once in the list.

    Comment

    • runes

      #3
      Re: Unique Elements in a List

      This is not the most beautiful idiom, but it works...

      d = {}
      for k in data:
      try:
      d[k] += 1
      except:
      d[k] = 1

      for k,v in d.items():
      if v == 1:
      print k

      Comment

      • Roie Kerstein

        #4
        Re: Unique Elements in a List

        superprad@gmail .com wrote:
        [color=blue]
        > Is there an easy way to grab the Unique elements from a list?
        >[/color]
        Yes.
        The set data type.
        Look in the reference of python 2.4.
        --
        Best regards
        Roie Kerstein

        Comment

        • James Stroud

          #5
          Re: Unique Elements in a List

          On Monday 09 May 2005 03:15 pm, superprad@gmail .com wrote:[color=blue]
          > Is there an easy way to grab the Unique elements from a list?
          > For Example:
          > data = [0.1,0.5,0.6,0.4 ,0.1,0.5,0.6,0. 9]
          >
          > what I am looking for is the unique elements 0.4 and 0.9 with their
          > index from the list.
          > Probably something like a Hash Table approach!!
          > I would like to get this done without unnecessary overhead.And the list
          > could be essentially anything strings,floats, int etc...
          >
          > Or is it already avaliable as an attr to a list or an array?
          > I dont remember seeing anything like that.[/color]

          from sets import Set

          data = [0.1,0.5,0.6,0.4 ,0.1,0.5,0.6,0. 9]

          [x for x in Set(data) if data.count(x) == 1]

          --
          James Stroud
          UCLA-DOE Institute for Genomics and Proteomics
          Box 951570
          Los Angeles, CA 90095


          Comment

          • bearophileHUGS@lycos.com

            #6
            Re: Unique Elements in a List

            runes:
            [color=blue]
            > d = {}
            > for k in data:
            > try:
            > d[k] += 1
            > except:
            > d[k] = 1
            >
            > for k,v in d.items():
            > if v == 1:
            > print k[/color]

            For this problem, if duplicated keys are common enough, this version is
            probably the faster.

            With this *different* problem, it seems that sometimes the faster
            solution is using "in":
            This entry got me thinking. Here's my 5 cents. In all cases the construct if key in dictionary: value = dictionary[key] else: value...


            Bye,
            Bearophile

            Comment

            • Michael J. Fromberger

              #7
              Re: Unique Elements in a List

              In article <1115676903.432 187.294910@z14g 2000cwz.googleg roups.com>,
              "superprad@gmai l.com" <superprad@gmai l.com> wrote:
              [color=blue]
              > Is there an easy way to grab the Unique elements from a list?
              > For Example:
              > data = [0.1,0.5,0.6,0.4 ,0.1,0.5,0.6,0. 9]
              >
              > what I am looking for is the unique elements 0.4 and 0.9 with their
              > index from the list.
              > Probably something like a Hash Table approach!!
              > I would like to get this done without unnecessary overhead.And the list
              > could be essentially anything strings,floats, int etc...
              >
              > Or is it already avaliable as an attr to a list or an array?
              > I dont remember seeing anything like that.
              >[/color]

              From your comments downthread, it seems you want to find those elements
              of the input sequence which occur exactly once, and return not only
              these elements, but also their positions.

              One reasonable solution might be as follows:

              def unique_elts(seq ):
              elts = {}
              for pos, elt in enumerate(seq):
              elts.setdefault (elt, []).append(pos)

              return [ (x, p[0]) for (x, p) in elts.iteritems( )
              if len(p) == 1 ]

              This returns a list of tuples of the form (x, pos), where x is an
              element of seq that occurs exactly once, and pos is its index in the
              original sequence. This implementation traverses the input sequence
              exactly once, and requires storage proportional to the length of the
              input.

              -M

              --
              Michael J. Fromberger | Lecturer, Dept. of Computer Science
              http://www.dartmouth.edu/~sting/ | Dartmouth College, Hanover, NH, USA

              Comment

              • Aahz

                #8
                Re: Unique Elements in a List

                In article <1115679662.521 731.297510@f14g 2000cwb.googleg roups.com>,
                runes <rune.strand@gm ail.com> wrote:[color=blue]
                >
                >This is not the most beautiful idiom, but it works...
                >
                >d = {}
                >for k in data:
                > try:
                > d[k] += 1
                > except:
                > d[k] = 1
                >
                >for k,v in d.items():
                > if v == 1:
                > print k[/color]

                Only if "works" does not include "order preserving".
                --
                Aahz (aahz@pythoncra ft.com) <*> http://www.pythoncraft.com/

                "And if that makes me an elitist...I couldn't be happier." --JMS

                Comment

                • Paul Rubin

                  #9
                  Re: Unique Elements in a List

                  "superprad@gmai l.com" <superprad@gmai l.com> writes:[color=blue]
                  > Is there an easy way to grab the Unique elements from a list?
                  > For Example:
                  > data = [0.1,0.5,0.6,0.4 ,0.1,0.5,0.6,0. 9][/color]

                  Untested: here's an iterator that gives you the index and value,
                  without reordering. Uses dicts instead of sets for backwards compatibility.

                  def unique_elements (data):
                  seen = {}
                  for n,x in data:
                  if x not in seen:
                  seen[x] = 1
                  yield n,x

                  Comment

                  • Rune Strand

                    #10
                    Re: Unique Elements in a List

                    You're right. I somehow missed the index part :-o. It's easy to fix
                    though.

                    Comment

                    • Fredrik Lundh

                      #11
                      Re: Unique Elements in a List

                      Paul Rubin wrote:
                      [color=blue][color=green]
                      > > Is there an easy way to grab the Unique elements from a list?
                      > > For Example:
                      > > data = [0.1,0.5,0.6,0.4 ,0.1,0.5,0.6,0. 9][/color]
                      >
                      > Untested: here's an iterator that gives you the index and value,
                      > without reordering. Uses dicts instead of sets for backwards compatibility.
                      >
                      > def unique_elements (data):
                      > seen = {}
                      > for n,x in data:
                      > if x not in seen:
                      > seen[x] = 1
                      > yield n,x[/color]

                      you forgot enumerate()

                      and if you fix that, you'll notice that the output doesn't quite match
                      the OP's spec:
                      [color=blue]
                      > For Example:
                      > data = [0.1,0.5,0.6,0.4 ,0.1,0.5,0.6,0. 9]
                      >
                      > what I am looking for is the unique elements 0.4 and 0.9 with their
                      > index from the list.[/color]

                      here's a straight-forward variation that gives the specified output,
                      in the original order:

                      def unique_elements (data):
                      count = {}
                      data = list(enumerate( data))
                      for n,x in data:
                      count[x] = count.setdefaul t(x, 0) + 1
                      for n,x in data:
                      if count[x] == 1:
                      yield x, n # value with index

                      depending on the data, it might be more efficient to store the
                      "last seen index" in a dictionary, and sort the result on the way
                      out (if necessary). something like

                      from operator import itemgetter

                      def unique_elements (data):
                      seen = {}; index = {}
                      for n, x in enumerate(data) :
                      if x in seen:
                      del index[x]
                      else:
                      index[x] = seen[x] = n
                      index = index.items()
                      index.sort(key= itemgetter(1)) # leave this out if order doesn't matter
                      return index

                      could work.

                      </F>



                      Comment

                      • Edvard Majakari

                        #12
                        Re: Unique Elements in a List

                        James Stroud <jstroud@mbi.uc la.edu> writes:
                        [color=blue]
                        > from sets import Set
                        >
                        > data = [0.1,0.5,0.6,0.4 ,0.1,0.5,0.6,0. 9]
                        >
                        > [x for x in Set(data) if data.count(x) == 1][/color]

                        Um.

                        ....I must have missed something, but I'll post nevertheless:

                        wouldn't just

                        [x for x in data if data.count(x) == 1]

                        suffice? it is also "stable" preserving order of items. Lemme demo:
                        [color=blue][color=green][color=darkred]
                        >>> [x for x in Set(data) if data.count(x) == 1][/color][/color][/color]
                        [0.9000000000000 0002, 0.4000000000000 0002]
                        [color=blue][color=green][color=darkred]
                        >>> [x for x in data if data.count(x) == 1][/color][/color][/color]
                        [0.4000000000000 0002, 0.9000000000000 0002]

                        Though I'll admit I also thought of Sets first, because I didn't remember
                        there was such a nice method list.count().

                        --
                        # Edvard Majakari Software Engineer
                        # PGP PUBLIC KEY available Soli Deo Gloria!
                        One day, when he was naughty, Mr Bunnsy looked over the hedge into Farmer
                        Fred's field and it was full of fresh green lettuces. Mr Bunnsy, however, was
                        not full of lettuces. This did not seem fair. --Mr Bunnsy has an adventure

                        Comment

                        • Max M

                          #13
                          Re: Unique Elements in a List

                          Fredrik Lundh wrote:
                          [color=blue]
                          > depending on the data, it might be more efficient to store the
                          > "last seen index" in a dictionary, and sort the result on the way
                          > out (if necessary). something like[/color]


                          form sets import Set

                          data = list(Set([0.1,0.5,0.6,0.4 ,0.1,0.5,0.6,0. 9]))


                          --

                          hilsen/regards Max M, Denmark


                          IT's Mad Science

                          Comment

                          • Edvard Majakari

                            #14
                            Re: Unique Elements in a List

                            "Michael J. Fromberger" <Michael.J.From berger@Clothing .Dartmouth.EDU> writes:
                            [color=blue]
                            > One reasonable solution might be as follows:
                            >
                            > def unique_elts(seq ):
                            > elts = {}
                            > for pos, elt in enumerate(seq):
                            > elts.setdefault (elt, []).append(pos)
                            >
                            > return [ (x, p[0]) for (x, p) in elts.iteritems( )
                            > if len(p) == 1 ][/color]

                            This is neat. Being lazy (the wrong way) I've never payed much attention why
                            would I need dict.setdefault () when I have things as dict.get(k, [def]). This
                            was a nice example of good use for setdefault() because it works like
                            dict.get() except it also sets the value if it didn't exist.

                            +1 IOTW (idiom of the week).

                            --
                            # Edvard Majakari Software Engineer
                            # PGP PUBLIC KEY available Soli Deo Gloria!

                            $_ = '45647661726420 4d616a616b61726 92c206120436872 69737469616e20' ; print
                            join('',map{chr hex}(split/(\w{2})/)),uc substr(crypt(60 281449,'es'),2, 4),"\n";

                            Comment

                            • Fredrik Lundh

                              #15
                              Re: Unique Elements in a List

                              Max M wrote:
                              [color=blue][color=green]
                              >> depending on the data, it might be more efficient to store the
                              >> "last seen index" in a dictionary, and sort the result on the way
                              >> out (if necessary). something like[/color]
                              >
                              > form sets import Set
                              >
                              > data = list(Set([0.1,0.5,0.6,0.4 ,0.1,0.5,0.6,0. 9]))[/color]

                              read the OP's spec again.

                              </F>



                              Comment

                              Working...