我有三个文本文件:
文件A:
13 abc
123 def
234 ghi
1234 jkl
12 mno
文件B:
12 abc
12 def
34 qwe
43 rty
45 mno
文件C:
12 abc
34 sdg
43 yui
54 poi
54 def
我想看看第二列中的所有值在文件之间匹配。如果第二列已排序,则以下代码有效。但是如果第二列未排序,我如何对第二列进行排序并比较文件?
fileA = open("A.txt",'r')
fileB = open("B.txt",'r')
fileC = open("C.txt",'r')
listA1 = []
for line1 in fileA:
listA = line1.split('\t')
listA1.append(listA)
listB1 = []
for line1 in fileB:
listB = line1.split('\t')
listB1.append(listB)
listC1 = []
for line1 in fileC:
listC = line1.split('\t')
listC1.append(listC)
for key1 in listA1:
for key2 in listB1:
for key3 in listC1:
if key1[1] == key2[1] and key2[1] == key3[1] and key3[1] == key1[1]:
print "Common between three files:",key1[1]
print "Common between file1 and file2 files:"
for key1 in listA1:
for key2 in listB1:
if key1[1] == key2[1]:
print key1[1]
print "Common between file1 and file3 files:"
for key1 in listA1:
for key2 in listC1:
if key1[1] == key2[1]:
print key1[1]