我想要比较列表,而下面的代码执行此操作并不完全符合我想要的目标。
目前它将输出:
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)
答案 0 :(得分:1)
鉴于
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']]
你希望它们成为集合,这样你就可以进行集合操作({1, 2, 3, 4} - {3, 4, 5, 6} == {1, 2}
),所以要设置它们:
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']]
TEST_1 = {tuple(frozenset(x.split(",")) for x in t) for t in TEST_1}
TEST_2 = {tuple(frozenset(x.split(",")) for x in t) for t in TEST_2}
请注意,我将每个['4', '6034', 'L', 'LALLAL,LAL']
转换为({'4'}, {'6034'}, {'L'}, {'LALLAL', 'LAL'})
,因为set
没有订单({'LALLAL', 'LAL'} == {'LAL', 'LALLAL'}
)。
我还使用了frozenset
和tuple
s因为它们是不可变的,因此可以进入集合,list
和普通集不能。
然后你可以打印出来:
print("ONLY IN TEST 1:")
for thing in TEST_1 - TEST_2:
print('\t'.join(",".join(x) for x in thing))
print()
print("ONLY IN TEST 2:")
for thing in TEST_2 - TEST_1:
print('\t'.join(",".join(x) for x in thing))
print()
print("IN BOTH:")
for thing in TEST_1 & TEST_2:
print('\t'.join(",".join(x) for x in thing))
#>>> ONLY IN TEST 1:
#>>> 5 4231 N AD
#>>>
#>>> ONLY IN TEST 2:
#>>> 5 4231 N PL
#>>> 6 5231 T PAL
#>>>
#>>> IN BOTH:
#>>> 1 1231 L LA
#>>> 3 1634 L T
#>>> 4 6034 L LAL,LALLAL
#>>> 1 1234 L T
#>>> 2 1434 A C