我正在同時從具有相同內容(單詞列表)的兩個文本文件中讀取行。Python中具有相同內容的2個文件中的2行的標識
peach
carrot
apple
lemon
我想檢查兩條同步線是否相同。如果不是,總體相似性會下降。由於這兩個文件是相同的,因此檢查身份應該導致100%的相似性。相反,我得到0%。
from itertools import izip, izip_longest
with open(r'file1.txt', "rb") as f1, open(r'file2.txt', "rb") as f2:
#initialize numerator & denominator values for calculating file similarity
nTotal = 4 #total number of lines in each file
nIdent = nTotal
for line1, line2 in izip_longest(f1, f2):
if((line1 is line2) is False):
nIdent -=1
similarity = nIdent/nTotal
爲什麼這些線不相同?
+用於簡化代碼 –