Nebie: list question, speed

Stephen D Evans stevee at recombinant.demon.co.uk
Sat May 5 15:04:45 EDT 2001


how long does this take ?

if ergline not in ergfield:
      ergfield.append(ergline)

If the order is not important, could you use a dictionary? e.g.

ergfield = {} # dictionary

# more code...

ergfield[ergline] = ergline

or

if not ergfield.has_key(ergline)
    ergfield[ergline] = ergline


Stephen D Evans


Werner Hoch wrote:

> Hello,
>
> I wrote a little programm parsing two textfiles together.
> The result is a list without uniq entries:
>
> So i wrote this to check if the entry already exists:
> --------
> if ergfield.count(ergline) == 0:
>       ergfield.append(ergline)
> ---------
> execution time is about 45 seconds
>
> an then I tried a second statment which is twice as fast as the first one:
> ------------
> try:
>       ergfield.index(ergline)
> except:
>       ergfield.append(ergline)
> ------------
> execution time is about 21 seconds
>
> I don't like the second solution because it uses the exeption handling like
> a if statement!
> Are there better ways to do this?
>
> best regards
> werner
>
> BTW: how can I convert an integer to a string?
> --
> werner.ho at gmx.de
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20010505/c1a1c0e0/attachment.html>


More information about the Python-list mailing list