Python 在少数列表中查找重复项

Python 在少数列表中查找重复项,python,list,position,duplicates,nested-loops,Python,List,Position,Duplicates,Nested Loops,在我的例子中,重复项不是一个在一个列表中重新出现的项目,而是另一个列表中相同位置的项目。例如: list1 = [1,2,3,3,3,4,5,5] list2 = ['a','b','b','c','b','d','e','e'] list3 = ['T1','T2','T3','T4','T3','T4','T5','T5'] 因此,在所有3个列表中,真实副本的位置是[2,4]和[6,7]。因为列表1中的3重复,所以列表2中的“b”与列表1中的“T3”位置相同。在第二种情况下,5、e、T5表

在我的例子中,重复项不是一个在一个列表中重新出现的项目,而是另一个列表中相同位置的项目。例如:

list1 = [1,2,3,3,3,4,5,5]
list2 = ['a','b','b','c','b','d','e','e']
list3 = ['T1','T2','T3','T4','T3','T4','T5','T5']
因此,在所有3个列表中,真实副本的位置是[2,4]和[6,7]。因为列表1中的3重复,所以列表2中的“b”与列表1中的“T3”位置相同。在第二种情况下,5、e、T5表示列表中相同位置的重复项。我很难一步“自动”呈现结果

1) 我在第一张单子上找到了重复的

# Find Duplicated part numbers (exact maches)
def list_duplicates(seq):
  seen = set()
  seen_add = seen.add
  # adds all elements it doesn't know yet to seen and all other to seen_twice
  seen_twice = set( x for x in seq if x in seen or seen_add(x) )
  # turn the set into a list (as requested)
  return list(seen_twice)
# List of Duplicated part numbers
D_list1 = list_duplicates(list1)
D_list2 = list_duplicates(list2)
2) 然后我找到给定副本的位置,并在第二个列表中查看该位置

# find the row position of duplicated part numbers
def list_position_duplicates(list1,n,D_list1):
    position = []    
    gen = (i for i,x in enumerate(data) if x == D_list1[n])
    for i in gen: position.append(i)
    return position    

# Actual calculation find the row position of duplicated part numbers, beginning and end 
lpd_part = list_position_duplicates(list1,1,D_list1)
start = lpd_part[0]
end = lpd_part[-1]

lpd_parent = list_position_duplicates(list2[start:end+1],0,D_list2)

因此,在第2步中,我需要将n(在列表中找到重复元素的位置)放入,我希望自动执行此步骤,以使重复元素的位置在列表中的相同位置。对于所有副本,在同一时间,而不是一个接一个的“手动”。我认为它只需要一个for循环或if循环,但我对Python还不熟悉,我尝试了许多组合,但都不起作用

您可以使用同一索引上所有3个列表中的项作为键,并将相应索引作为值存储(在列表中)。如果对于任何键,列表中存储的索引超过1个,则该索引是重复的:

from itertools import izip

def solve(*lists):
  d = {}
  for i, k in enumerate(izip(*lists)):
    d.setdefault(k, []).append(i)
  for k, v in d.items():
    if len(v) > 1:
      print k, v

solve(list1, list2, list3)
#(3, 'b', 'T3') [2, 4]
#(5, 'e', 'T5') [6, 7]