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'] [EMAIL PROTECTED] ~ $ 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 [EMAIL PROTECTED] ~ $ 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.
pgp7Ajb86UhaT.pgp
Description: PGP signature
-- http://mail.python.org/mailman/listinfo/python-list