In article <[EMAIL PROTECTED]>,
 "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:

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

>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
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to