Unique Elements in a List

Max M maxm at mxm.dk
Tue May 10 07:52:30 EDT 2005


Fredrik Lundh wrote:
> Max M wrote:
> 
> 
>>>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
>>
>>form sets import Set
>>
>>data = list(Set([0.1,0.5,0.6,0.4,0.1,0.5,0.6,0.9]))
> 
> 
> read the OP's spec again.

Well if you want to haggle over minor details like a spec, it's easy to 
be critical!


data = [0.1,0.5,0.6,0.4,0.1,0.5,0.6,0.9]

freqs = {}
for position in xrange(len(data)):
     freqs.setdefault(data[position], []).append(position)
unique = [(value, positions[0]) for (value, positions) in freqs.items()
           if len(positions) == 1]



-- 

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science



More information about the Python-list mailing list