且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python:如果包含/不包含顺序相同,则从列表中删除子列表

更新时间:2023-11-25 22:57:22

这将保留列表和子列表的顺序,并可能在子列表中重复:

this will preserve the order of list and sublists, with possible duplicates in sublists:

y, s = [], set()
for t in x:
    w = tuple(sorted(t))
    if not w in s:
        y.append(t)
        s.add(w)

如果

x = [[1,2],[3,4],[5,6],[2,1,1],[2,1],[7,8],[4,3],[1,2,1]]

那么y将是:

[[1, 2], [3, 4], [5, 6], [2, 1, 1], [7, 8]]