A Mountain of Perl Books + Python Advocacy
erik_wilsher at my-deja.com
erik_wilsher at my-deja.com
Thu May 11 07:34:57 EDT 2000
In article <ur9bc7i3p.fsf at ctwd0143.fitlinxx.com>,
David Bolen <db3l at fitlinxx.com> wrote:
> tsummerfelt1 at myspleenhome.com (tony summerfelt)
writes:
>
> > i think something like the following is
pretty hard to beat:
> >
> > #!/usr/bin/perl
...lots of cryptic perl code, but I *guess* the
purpose was to read from a file and print out a
sorted, unique list of the input file.
>
> I guess it depends on what "beat" means - I
agree with a previous
>.....
> #!/usr/bin/env python
>
> import sys
>
> input = open(sys.argv[1])
> output = open(sys.argv[2],'w')
>
> uniq = []
> lastline = None
>
> while 1:
> curline = input.readline()
> if not curline: break
>
> if curline != lastline:
> uniq.append(curline)
> lastline = curline
>
> uniq.sort()
> output.writelines(uniq)
>
Having read this, and also noticed a request for
some sort of "unique" flag for lists in the
python todo-list, I added a sortlist class to my
bisect module.
Using this class we can reduce the above program
to:
--------
#!/usr/bin/env python
"uniq.py --Read lines from input file, and write
sorted output to file"
import fileinput,bisect,sys
uniq=bisect.sortlist(fileinput.input(sys.argv[1]))
open(sys.argv[2],'w').writelines(uniq.list())
--------
Q: I have modified the bisect module somewhat
(doc strings, find function, sortlist class).
What should be done with this? Vault? Patch list?
Erik Wilsher
Sent via Deja.com http://www.deja.com/
Before you buy.
More information about the Python-list
mailing list