我有三维列表。我需要删除重复的元素。是否有可能在没有遍历或更多pythonic方式的情况下过滤重复元素。 在下面的例子中,890和930是重复的。
coords = [[(335, 270), (365, 270), (365, 410), (335, 410), (335, 270)],
[(755, 270), (785, 270), (785, 410), (755, 410), (755, 270)],
[(615,250), (645, 250), (645, 390), (615, 390), (615, 250)],
[(195, 250),(225, 250), (225, 390), (195, 390), (195, 250)],
[(890, -5), (930, -5), (930, 705), (890, 705), (890, -5)],
[(610, -5), (650, -5), (650, 705), (610, 705), (610, -5)],
[(750, -5), (790, -5), (790, 705), (750, 705), (750, -5)],
[(470, -5), (510, -5), (510, 705), (470, 705), (470, -5)],
[(890, -5), (930, -5), (930, 705), (890, 705), (890, -5)],
[(610, -5), (650, -5), (650, 705), (610, 705), (610, -5)],
[(750, -5), (790, -5), (790, 705), (750, 705), (750, -5)],
[(470, -5), (510, -5), (510, 705), (470, 705), (470, -5)],
[(470, -5), (510, -5), (510, 705), (470, 705), (470, -5)],
[(190, -5), (230, -5), (230, 705), (190, 705), (190, -5)],
[(330, -5), (370, -5), (370, 705), (330, 705), (330, -5)],
[(50, -5), (90, -5), (90, 705), (50, 705), (50, -5)]]
答案 0 :(得分:0)
您可以为list(set(sublist))
中的每个列表致电coords
。保证结果列表仅包含唯一元素,但不保留原始订单。
>>> [list(set(x)) for x in coords]
[[(365, 270), (335, 270), (365, 410), (335, 410)],
[(785, 410), (755, 410), (785, 270), (755, 270)],
[(645, 390), (615, 390), (645, 250), (615, 250)],
[(225, 250), (195, 250), (225, 390), (195, 390)],
[(930, -5), (890, -5), (890, 705), (930, 705)],
[(650, -5), (610, -5), (610, 705), (650, 705)],
[(750, -5), (790, -5), (790, 705), (750, 705)],
[(470, -5), (510, -5), (510, 705), (470, 705)],
[(930, -5), (890, -5), (890, 705), (930, 705)],
[(650, -5), (610, -5), (610, 705), (650, 705)],
[(750, -5), (790, -5), (790, 705), (750, 705)],
[(470, -5), (510, -5), (510, 705), (470, 705)],
[(470, -5), (510, -5), (510, 705), (470, 705)],
[(190, -5), (230, -5), (230, 705), (190, 705)],
[(370, -5), (330, -5), (330, 705), (370, 705)],
[(90, -5), (50, -5), (50, 705), (90, 705)]]
如果您必须维护订单,则需要维护一个集合,将所有看到的元组添加到:
>>> result = []
>>> for subl in coords:
... seen = set()
... result.append([x for x in subl if x not in seen and not seen.add(x)])
...
>>> result
[[(335, 270), (365, 270), (365, 410), (335, 410)],
[(755, 270), (785, 270), (785, 410), (755, 410)],
[(615, 250), (645, 250), (645, 390), (615, 390)],
[(195, 250), (225, 250), (225, 390), (195, 390)],
[(890, -5), (930, -5), (930, 705), (890, 705)],
[(610, -5), (650, -5), (650, 705), (610, 705)],
[(750, -5), (790, -5), (790, 705), (750, 705)],
[(470, -5), (510, -5), (510, 705), (470, 705)],
[(890, -5), (930, -5), (930, 705), (890, 705)],
[(610, -5), (650, -5), (650, 705), (610, 705)],
[(750, -5), (790, -5), (790, 705), (750, 705)],
[(470, -5), (510, -5), (510, 705), (470, 705)],
[(470, -5), (510, -5), (510, 705), (470, 705)],
[(190, -5), (230, -5), (230, 705), (190, 705)],
[(330, -5), (370, -5), (370, 705), (330, 705)],
[(50, -5), (90, -5), (90, 705), (50, 705)]]