[英]how to remove partial duplicates in a list of lists
I have a list of lists where it looks like 我有一个看起来像清单的清单
[[1,'a',2],[1,'b',2],[1,'a',3]]
I want to remove the item from the list if the second element in the list of lists are the same (eg they are both a
) 如果列表列表中的第二个元素相同(例如,它们都是
a
),我想从列表中删除该项目
I want to create output that looks like: 我想创建如下输出:
[[1,'a',2],[1,'b',2]]
where it grabs the first one in the list of the duplicates. 它在重复项列表中获取第一个。
that's a variant of How do you remove duplicates from a list whilst preserving order? 这是如何在保留订单的同时从列表中删除重复项的变体? .
。
You can use a marker set to track the already appended sublists since strings are immutable so hashable & storable in a set
: 您可以使用标记集来跟踪已附加的子列表,因为字符串是不可变的,因此在
set
可哈希和可存储:
lst = [[1,'a',2],[1,'b',2],[1,'a',3]]
marker_set = set()
result = []
for sublist in lst:
second_elt = sublist[1]
if second_elt not in marker_set:
result.append(sublist)
marker_set.add(second_elt)
print(result)
prints: 印刷品:
[[1, 'a', 2], [1, 'b', 2]]
(using a marker set and not a list allows an average O(1)
lookup instead of O(N)
) (使用标记集而不是列表允许平均
O(1)
查找而不是O(N)
)
You can use a dictionary where the second element is the key, on the reverse of the list, to drop duplicates: 您可以使用字典,其中第二个元素是键,位于列表的反面,以删除重复项:
dct = {j: (i, k) for i, j, k in reversed(L)}
{'a': (1, 2), 'b': (1, 2)}
Getting the result back as a list: 以列表形式返回结果:
[[i, j, k] for j, (i, k) in dct.items()]
[[1, 'a', 2], [1, 'b', 2]]
While this solution will always keep the first occurence of a duplicate, the relative order of elements is not guaranteed in the final result. 尽管此解决方案将始终保留重复项的首次出现,但不能保证最终结果中元素的相对顺序。
lst = [[1,'a',2],[1,'b',2],[1,'a',3]]
res = []
for i in lst:
if not any(i[1] in j for j in res):
res.append(i)
print(res)
# [[1, 'a', 2], [1, 'b', 2]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.