newbie:unique problem

Heiko Wundram modelnine at ceosg.de
Thu Mar 17 16:29:41 EST 2005


On Thursday 17 March 2005 20:08, Leeds, Mark wrote:
> But, I also want it to get rid of the AAA KP because
> there are two AAA's even though the last two letters
> are different. It doesn't matter to me which one
> is gotten rid of but I don't know how to change
> the function to handle this ? I have a feeling
> it's not that hard though ? Thanks.

Doing the same thing Brian van den Brook did with sets (also for 2.4 only):

def uniqueItems(oldlist,comppos=3):
    rv = {}
    for i in reversed(oldlist):
        rv[i[:comppos]] = i
    return rv.values()

>>> uniqueItems(["AAA BC","BBB KK","CCC TD","AAA KP","CCC TD"])
['AAA BC', 'BBB KK', 'CCC TD']

heiko at heiko ~ $ python2.4 /usr/local/lib/python2.4/timeit.py -s "import test; 
uniqueItems = test.uniqueItems; uniqueItemsBrian = test.uniqueItemsBrian" 
"uniqueItemsBrian(uniqueItems)"
100000 loops, best of 3: 13.8 usec per loop

heiko at heiko ~ $ python2.4 /usr/local/lib/python2.4/timeit.py -s "import test; 
uniqueItems = test.uniqueItems; uniqueItemsHeiko = test.uniqueItemsHeiko" 
"uniqueItemsHeiko(uniqueItems)"
100000 loops, best of 3: 9.28 usec per loop

Seems like the dictionary solution is faster, at least for n=3. Do your own 
tests... ;)

-- 
--- Heiko.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20050317/bea66fa1/attachment.sig>


More information about the Python-list mailing list