我感觉这段 Python 代码可以大大缩短,但我几乎总是倾向于回退到编写 C 样式的布局。您认为缩短它的最佳方法是什么?可读性是一种奖励,而不是要求。
def compfiles(file1, file2):
linecnt = 0
for line1 in open(file1):
line1 = line1.strip()
hit = False
for line2 in open(file2):
line2 = line2.strip()
if line2 == line1:
hit = True
break
if not hit:
print("Miss: file %s contains '%s', but file %s does not!" % (file1, line1, file2))
linecnt += 1
print("%i lines compared between %s and %s." % (linecnt, file1, file2))
fn = ["file1.txt", "file2.txt"]
compfiles(fn[0], fn[1])
compfiles(fn[1], fn[0])