完全不会perl,求大神帮忙改写以下的perl script成python,如果能优化以下代码就更好了,感激不尽。谢谢
use warnings;
use strict;
if (scalar(@ARGV) != 2) {
print "Expecting 2 parameters: original file list and the new file list to compare to.\n";
} else {
open my $file1, '<', $ARGV[0] or die "Cannot open file: $!";
open my $file2, '<', $ARGV[1] or die "Cannot open file: $!";
open my $newfile, '>', 'output.txt';
my @oldlist = <$file1>;
my @newlist = <$file2>;
my $i = 0;
my $isEqual = 'false'; # A flag to identify new/modified entries #
# sorting the two arrays to make the comparison more efficient.
@oldlist = sort @oldlist;
@newlist = sort @newlist;
while (scalar(@newlist) != 0) {
$isEqual = 'false';
for (my $i=0; $i<scalar(@oldlist); $i++) {
if ($oldlist[$i] eq $newlist[0]) {
$isEqual = 'true';
last; # No more comparison is needed for this entry. Jump out of the loop #
}
}
if ($isEqual eq 'false') {
print $newfile shift @newlist;
} else {
shift @newlist;
splice(@oldlist,$i,1);
}
}
close $file1 or die "Cannot close file: $!";
close $file2 or die "Cannot close file: $!";
}
exit;