Re: Unique Elements in a List

2005-05-17 Thread Nyet Ya Koshka
> 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 ] > Minor tweak to conserve space:

Re: Unique Elements in a List

2005-05-15 Thread Facundo Batista
On 5/9/05, James Stroud <[EMAIL PROTECTED]> wrote: > > Is there an easy way to grab the Unique elements from a list? >>> from sets import Set as set >>> data = [0.1,0.5,0.6,0.4,0.1,0.5,0.6,0.9] >>> for x in set(data): ... print x ... 0.5 0.9 0.6 0.4 0.1 .Facundo Blog: http://www.taniqu

Re: Unique Elements in a List

2005-05-12 Thread Andrew Dalke
Scott David Daniels wrote: > Again polynomial, not exponential time. Note that there is no > polynomial time algorithm with (k < 1), since it takes O(n) time > to read the problem. Being a stickler (I develop software after all :) quantum computers can do better than that. For example, Grover's

Re: Unique Elements in a List

2005-05-12 Thread Edvard Majakari
Scott David Daniels <[EMAIL PROTECTED]> writes: > Normally this is called a polynomial, rather than exponential increase. > Exponential increases are typically of the form (C^N) (they are all > equivalent). > Polynomial times are hallways characterized by their largest exponent, > So you never cal

Re: Unique Elements in a List

2005-05-12 Thread Scott David Daniels
Edvard Majakari wrote: > I realized that, but maybe I should've pointed it out too. For the OP if > he/she is unaware - notation O(N^2) (big O n squared) means the computing time > of the algorithm increases exponentially (where exponent is 2) relative to the > size of the input. Normally this is c

Re: Unique Elements in a List

2005-05-12 Thread Edvard Majakari
[EMAIL PROTECTED] (Aahz) writes: >>[x for x in data if data.count(x) == 1] >> >>suffice? it is also "stable" preserving order of items. Lemme demo: > > Only for small datasets -- this is an O(N^2) algorithm. I realized that, but maybe I should've pointed it out too. For the OP if he/she is unawa

Re: Unique Elements in a List

2005-05-10 Thread Steven Bethard
[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. This is basically the same as Fredrik Lundh's solution,

Re: Unique Elements in a List

2005-05-10 Thread Aahz
In article <[EMAIL PROTECTED]>, Edvard Majakari <[EMAIL PROTECTED]> wrote: >James Stroud <[EMAIL PROTECTED]> writes: >> >> 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] > >Um. > >...I must have missed something, but I'll post

Re: Unique Elements in a List

2005-05-10 Thread Paul Rubin
"Fredrik Lundh" <[EMAIL PROTECTED]> writes: > you forgot enumerate() Oh, oops. > and if you fix that, you'll notice that the output doesn't quite match > the OP's spec: > > > what I am looking for is the unique elements 0.4 and 0.9 with their > > index from the list. Oh, I see, he wante

Re: Unique Elements in a List

2005-05-10 Thread Max M
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]))

Re: Unique Elements in a List

2005-05-10 Thread Bengt Richter
On 9 May 2005 15:36:37 -0700, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: >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. > You want to be careful of your definitions, es

Re: Unique Elements in a List

2005-05-10 Thread Fredrik Lundh
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.

Re: Unique Elements in a List

2005-05-10 Thread Edvard Majakari
"Michael J. Fromberger" <[EMAIL PROTECTED]> writes: > 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() >

Re: Unique Elements in a List

2005-05-10 Thread Max M
Fredrik Lundh 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])) -- hilsen/regards Max M, D

Re: Unique Elements in a List

2005-05-10 Thread Edvard Majakari
James Stroud <[EMAIL PROTECTED]> writes: > 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] 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 a

Re: Unique Elements in a List

2005-05-10 Thread Fredrik Lundh
Paul Rubin 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] > > Untested: here's an iterator that gives you the index and value, > without reordering. Uses dicts instead of sets for backwards compatibility. > >

Re: Unique Elements in a List

2005-05-09 Thread Rune Strand
You're right. I somehow missed the index part :-o. It's easy to fix though. -- http://mail.python.org/mailman/listinfo/python-list

Re: Unique Elements in a List

2005-05-09 Thread Paul Rubin
"[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes: > 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] Untested: here's an iterator that gives you the index and value, without reordering. Uses dicts instead of sets for backwards c

Re: Unique Elements in a List

2005-05-09 Thread Aahz
In article <[EMAIL PROTECTED]>, runes <[EMAIL PROTECTED]> wrote: > >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 Only if "works" does not include

Re: Unique Elements in a List

2005-05-09 Thread Michael J. Fromberger
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.

Re: Unique Elements in a List

2005-05-09 Thread bearophileHUGS
runes: > d = {} > for k in data: > try: > d[k] += 1 > except: > d[k] = 1 > > for k,v in d.items(): > if v == 1: > print k For this problem, if duplicated keys are common enough, this version is probably the faster. With this *different* problem, it seems that

Re: Unique Elements in a List

2005-05-09 Thread runes
Your set-approach is very nice, but if the list of data is huge, it is rather slow because you'll have to loop through the data list and count every member. -- http://mail.python.org/mailman/listinfo/python-list

Re: Unique Elements in a List

2005-05-09 Thread James Stroud
On Monday 09 May 2005 03:15 pm, [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

Unique Elements in a List

2005-05-09 Thread James Stroud
On Monday 09 May 2005 03:15 pm, [EMAIL PROTECTED] wrote: > Is there an easy way to grab the Unique elements from a list? 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

Re: Unique Elements in a List

2005-05-09 Thread Roie Kerstein
[EMAIL PROTECTED] wrote: > Is there an easy way to grab the Unique elements from a list? > Yes. The set data type. Look in the reference of python 2.4. -- Best regards Roie Kerstein -- http://mail.python.org/mailman/listinfo/python-list

Re: Unique Elements in a List

2005-05-09 Thread [EMAIL PROTECTED]
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. -- http://mail.python.org/mailman/listinfo/python-list

Re: Unique Elements in a List

2005-05-09 Thread runes
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 -- http://mail.python.org/mailman/listinfo/python-list

Unique Elements in a List

2005-05-09 Thread [EMAIL PROTECTED]
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 unneces