drochom wrote:
> i suppose this one is faster (but in most cases efficiency doesn't
> matter)
>
def stable_unique(s):
>
> e = {}
> ret = []
> for x in s:
> if not e.has_key(x):
> e[x] = 1
> ret.append(x)
> retur
Thanks for all the information.
And now I understand the timeit module ;)
GC-Martijn
--
http://mail.python.org/mailman/listinfo/python-list
thanks, nice job. but this benchmark is pretty deceptive:
try this:
(definition of unique2 and unique3 as above)
>>> import timeit
>>> a = range(1000)
>>> t = timeit.Timer('unique2(a)','from __main__ import unique2,a')
>>> t2 = timeit.Timer('stable_unique(a)','from __main__ import stable_unique,a
Ow thanks , i'm I newbie and I did this test. (don't know if this is
the best way to do a small speed test)
import timeit
def unique2(keys):
unique = []
for i in keys:
if i not in unique:unique.append(i)
return unique
def unique3(s):
e = {}
ret = []
for x in s:
i suppose this one is faster (but in most cases efficiency doesn't
matter)
>>> def stable_unique(s):
e = {}
ret = []
for x in s:
if not e.has_key(x):
e[x] = 1
ret.append(x)
return ret
cheers,
przemek
there wasn't any information about ordering...
maybe i'll find something better which don't destroy original ordering
regards
przemek
--
http://mail.python.org/mailman/listinfo/python-list
Look at the code below
def unique(s):
return list(set(s))
def unique2(keys):
unique = []
for i in keys:
if i not in unique:unique.append(i)
return unique
tmp = [0,1,2,4,2,2,3,4,1,3,2]
print tmp
print unique(tmp)
print unique2(tmp)
--
[0, 1, 2, 4, 2
Rubinho napisal(a):
> I've a list with duplicate members and I need to make each entry
> unique.
>
hi,
other possibility (my newest discovery:) )
>>> a = [1,2,2,4,2,1,3,4]
>>> unique = d.fromkeys(a).keys()
>>> unique
[1, 2, 3, 4]
regards
przemek
--
http://mail.python.org/mailman/listinfo/pyth
przemek drochomirecki wrote:
> def unique(s):
> e = {}
> for x in s:
> if not e.has_key(x):
>e[x] = 1
> return e.keys()
This is basically identical in functionality to the code:
def unique(s):
return list(set(s))
And with the new-and-improved C implementation of sets comin
This works too, if speed isn't your thing..
>> a = [ 1,2,3,2,6,1,3,4,1,7,5,6,7]
>> a = dict( ( (i,None) for i in a)).keys()
a
[1, 2, 3, 4, 5, 6, 7]
--
http://mail.python.org/mailman/listinfo/python-list
> I've a list with duplicate members and I need to make each entry
> unique.
>
> I've come up with two ways of doing it and I'd like some input on what
> would be considered more pythonic (or at least best practice).
>
> Method 1 (the traditional approach)
>
> for x in mylist:
> if mylist.count
Steven D'Aprano wrote:
>
>
> Don't imagine, measure.
>
> Resist the temptation to guess. Write some test functions and time the two
> different methods. But first test that the functions do what you expect:
> there is no point having a blindingly fast bug.
Thats is absolutely correct. Although
On Wed, 14 Sep 2005 13:28:58 +0100, Will McGugan wrote:
> Rubinho wrote:
>> I can't imagine one being much faster than the other except in the case
>> of a huge list and mine's going to typically have less than 1000
>> elements.
>
> I would imagine that 2 would be significantly faster.
Don't
Rubinho wrote:
> I can't imagine one being much faster than the other except in the case
> of a huge list and mine's going to typically have less than 1000
> elements.
To add to what others said, I'd imagine that the technique that's going
to be fastest is going to depend not only on the lengt
I do this:
def unique(keys):
unique = []
for i in keys:
if i not in unique:unique.append(i)
return unique
I don't know what is faster at the moment.
--
http://mail.python.org/mailman/listinfo/python-list
<[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
>I do this:
>
> def unique(keys):
>unique = []
>for i in keys:
>if i not in unique:unique.append(i)
>return unique
>
> I don't know what is faster at the moment.
This is quadratic, O(n^2), in the length n of the list
Peter Otten wrote:
> Rubinho wrote:
>
> > I've a list with duplicate members and I need to make each entry
> > unique.
> >
> > I've come up with two ways of doing it and I'd like some input on what
> > would be considered more pythonic (or at least best practice).
> >
> > Method 1 (the traditional
Rubinho wrote:
> I've a list with duplicate members and I need to make each entry
> unique.
>
> I've come up with two ways of doing it and I'd like some input on what
> would be considered more pythonic (or at least best practice).
>
> Method 1 (the traditional approach)
>
> for x in mylist:
>
Rubinho wrote:
> I've a list with duplicate members and I need to make each entry
> unique.
>
> I've come up with two ways of doing it and I'd like some input on what
> would be considered more pythonic (or at least best practice).
>
> Method 1 (the traditional approach)
>
> for x in mylist:
>
Am Wed, 14 Sep 2005 04:38:35 -0700 schrieb Rubinho:
> I've a list with duplicate members and I need to make each entry
> unique.
>
> I've come up with two ways of doing it and I'd like some input on what
> would be considered more pythonic (or at least best practice).
> mylist = set(mylist)
> my
I've a list with duplicate members and I need to make each entry
unique.
I've come up with two ways of doing it and I'd like some input on what
would be considered more pythonic (or at least best practice).
Method 1 (the traditional approach)
for x in mylist:
if mylist.count(x) > 1:
21 matches
Mail list logo