说我有一个列表看起来像这样:
a = [(1,2),(3,1),(2,1),(4,5),(9,3),(1,3)]
然后,我希望看到这样的东西:
b = [(1,2),(3,1),(4,5),(9,3)]
非常感谢!
解决方法:
b = []
seen = set()
for t in a:
s = tuple(sorted(t))
if s not in seen:
seen.add(s)
b.append(t)
要么
seen = set()
b = [t for t in a if tuple(sorted(t)) not in seen and not seen.add(tuple(sorted(t)))]