A Mountain of Perl Books + Python Advocacy

Andrew Dalke dalke at acm.org
Wed May 10 03:38:24 CEST 2000


Ben Wolfson wrote:
>lines.sort()
>for line in lines:
>   if line not in uniqlines:
>      uniqlines.append(line)
>      out.write(line)
>out.close()
>


Since you've sorted the lines, you don't need the "line not in ..."
That gives you order n-squared performance.  Since you already sorted
the lines, a better one is:

lines.sort()
prev = None
for line in lines:
  if line != prev:
    out.write(line)
    prev = line

                    Andrew






More information about the Python-list mailing list