这里有一个更具活力的解决方案。为了得到一个不同的测试用例,我稍微修改了你的输入。这对副本有效,而且在O(N)时间内也有效。
from collections import defaultdict
L1 = [('G', 'T'), ('T', 'T'), ('T', 'U'), ('U', 'I'), ('I', 'P'), ('U', 'I')]
L2 = [('E', 'G'), ('G', 'T'), ('T', 'P'), ('U', 'I')]
S1 = defaultdict(list)
S2 = defaultdict(list)
for indx, element in enumerate(L1):
S1[element].append(indx)
for indx, element in enumerate(L2):
S2[element].append(indx)
duplicate_elements = set(S1).intersection(set(S2))
print("Mutual elements:", *[(S1[dup], S2[dup]) for dup in duplicate_elements])
输出:
Mutual elements: ([0], [1]) ([3, 5], [3])