我想比较列表,而下面的代码执行此操作并不能完全实现我想要实现的目标。
目前它将输出:
Lines only found in TEST_1:
4 6034 L LAL,LALLAL
5 4231 N AD
Lines only found in TEST_2:
4 6034 L LALLAL,LAL
5 4231 N PL
6 5231 T PAL
Lines match in both:
1 1231 L LA
1 1234 L T
2 1434 A C
3 1634 L T
我想要的是:
Lines only found in TEST_1:
5 4231 N AD
Lines only found in TEST_2:
5 4231 N PL
6 5231 T PAL
Lines match in both:
1 1231 L LA
1 1234 L T
2 1434 A C
3 1634 L T
4 6034 L LAL,LALLAL
如何使最后一列中的数据顺序无关紧要?所以4 6034 L LAL,LALLAL
匹配4 6034 L LALLAL,LAL
.
示例代码:
TEST_1 = [['1', '1231', 'L', 'LA'],['1', '1234', 'L', 'T'],
['2', '1434', 'A', 'C'],['3', '1634', 'L', 'T'],
['4', '6034', 'L', 'LAL,LALLAL'],['5', '4231', 'N', 'AD']]
TEST_2 = [['1', '1231', 'L', 'LA'],['1', '1234', 'L', 'T'],
['2', '1434', 'A', 'C'],['3', '1634', 'L', 'T'],
['4', '6034', 'L', 'LALLAL,LAL'],['5', '4231', 'N', 'PL'],
['6', '5231', 'T', 'PAL']]
MATCH_1 = []
MATCH_2 = []
NO_MATCH_1 = []
NO_MATCH_2 = []
ENTRY = [TEST_1, TEST_2, MATCH_1, MATCH_2, NO_MATCH_1, NO_MATCH_2]
for i in range(0, 2):
for word in ENTRY[i]:
if word not in ENTRY[1-i]:
ENTRY[4+i].append(word)
else:
ENTRY[2+i].append(word)
print 'Lines only found in TEST_1:\t'
for word in ENTRY[4]:
print '\t'.join(word)
print '\nLines only found in TEST_2:\t'
for word in ENTRY[5]:
print '\t'.join(word)
print '\nLines match in both:\t'
for word in ENTRY[2]:
print '\t'.join(word)