> 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:
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
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
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
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
[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
[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,
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
"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
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]))
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
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.
"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()
>
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
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
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.
>
>
You're right. I somehow missed the index part :-o. It's easy to fix
though.
--
http://mail.python.org/mailman/listinfo/python-list
"[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
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
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.
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
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
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
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
[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
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
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
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
28 matches
Mail list logo