我想弄清楚如何在序列列表中删除“部分”重复项。特别是,如果列表中两个(或更多)序列的前两个元素相同,我想在列表中只保留其中一个序列。我在python 2.7中编码。
例如,如果我有: [(1, 2, 'keep'), (3, 4, 'also keep'), (1, 2, 'remove'), (3, 4, 'also remove')]
我想要返回
[(1, 2, 'keep'), (3, 4, 'also keep')]
答案 0 :(得分:0)
这是两个非常相似的功能,可以满足您的需求:
def partial_duplicates1(l):
check_list = []
final_list = []
for item in l:
first_two_elems = item[:2]
if first_two_elems not in check_list:
final_list.append(item)
check_list.append(first_two_elems)
return final_list
def partial_duplicates2(l):
already_included = {item[:2]: False for item in l}
final_list = []
for item in l:
first_two_elems = item[:2]
if not already_included[first_two_elems]:
final_list.append(item)
already_included[first_two_elems] = True
return final_list