A Mountain of Perl Books + Python Advocacy

Mike Fletcher mfletch at tpresence.com
Mon May 8 11:28:22 EDT 2000


Hmm...  following is untested.  I think you'll find in general that for
terseness in extremely small programmes, Python is not going to beat Perl
(which, as you know, many consider a blessing :) ).

Enjoy yourself,
Mike

8<__________ uniq.py _________
import sys, fileinput

usage = '''uniq.py [input] [output]
Attempt to find unique lines within input and write those lines
to output.
'''

try:
	input = fileinput.FileInput( sys.argv[1] )
except IOError, value:
	raise SystemExit( "Unable to open input file %s"%(sys.argv[1]) )
except IndexError:
	input = sys.stdin.readlines()

lines = {}
for line in input:
	# increment count, use 0 if not already present
	lines[ line] = lines.get( line, 0) + 1

try:
	output = open( sys.argv[2], 'w')
except IOError, value:
	raise SystemExit( "Unable to open output file %s"%(sys.argv[2]) )
except IndexError:
	output = sys.stdout

for line, count in lines.items():
	if count == 1:
		output.write( line )



-----Original Message-----
From: tsummerfelt1 at myspleenhome.com
[mailto:tsummerfelt1 at myspleenhome.com]
Sent: Monday, May 08, 2000 10:19 AM
To: python-list at python.org
Subject: Re: A Mountain of Perl Books + Python Advocacy


...
i think something like the following is pretty hard to beat:

#!/usr/bin/perl

open(in,$ARGV[0]) || die("can't open $ARGV[0]");
open(out,">$ARGV[1]) || die("can't open $ARGV[1]);

while (<in>)
{
  unless($i,{$_}++) {push(@uniq,$_);}
}
print out (sort(@uniq));
...




More information about the Python-list mailing list