2

我试图一个接一个地删除一组不同的元组列表。列表如下所示:

A = [
     (('X','Y','Z',2,3,4), ('A','B','C',5,10,11)),
     (('A','B','C',5,10,11), ('X','Y','Z',2,3,4)),
     (('T','F','J',0,1,0), ('H','G','K',2,8,7)),
     ...                                          ]

B = [
     (('X','Y','Z',0,0,0), ('A','B','C',3,3,2)),
     (('A','B','C',3,3,2), ('X','Y','Z',0,0,0)),
     (('J','K','L',5,4,3), ('V','T','D',5,10,12)),
     ...                                          ]

我正在运行(例如列表 A):

from collections import OrderedDict
values = [[x,y] for x, y in OrderedDict.fromkeys(frozenset(x) for x in A)]

我会得到:

 A = [
     (('X','Y','Z',2,3,4), ('A','B','C',5,10,11)),
     (('T','F','J',0,1,0), ('H','G','K',2,8,7)),
     ...                                         ]

但是,如果我对 B 重复,我可能会选择第二个元组而不是第一个:

B = [
     (('A','B','C',3,3,2), ('X','Y','Z',0,0,0)),
     (('J','K','L',5,4,3), ('V','T','D',5,10,12)),
     ...                                         ]

理想情况下,B 应该是:

B = [
     (('X','Y','Z',0,0,0), ('A','B','C',3,3,2)),
     (('J','K','L',5,4,3), ('V','T','D',5,10,12)),
     ...                                          ] 

对于字符串序列,我需要它们相同,因为我将使用它们来连接 A、B 等中的浮点数。我很高兴知道是否有办法让去重列表的选择方法保持不变。谢谢!

4

1 回答 1

3

要保持先前的顺序,请遍历这些对并跟踪您所看到的内容。仅包含尚未见过的元素:

def dedup(lst):
    seen = set()
    result = []
    for item in lst:
        fs = frozenset(item)
        if fs not in seen:
            result.append(item)
            seen.add(fs)
    return result

例子:

>>> A = [
...      (('X','Y','Z',2,3,4), ('A','B','C',5,10,11)),
...      (('A','B','C',5,10,11), ('X','Y','Z',2,3,4)),
...      (('T','F','J',0,1,0), ('H','G','K',2,8,7)),
...     ]
>>> pprint.pprint(dedup(A))
[(('X', 'Y', 'Z', 2, 3, 4), ('A', 'B', 'C', 5, 10, 11)),
 (('T', 'F', 'J', 0, 1, 0), ('H', 'G', 'K', 2, 8, 7))]
>>> B = [
...      (('X','Y','Z',0,0,0), ('A','B','C',3,3,2)),
...      (('A','B','C',3,3,2), ('X','Y','Z',0,0,0)),
...      (('J','K','L',5,4,3), ('V','T','D',5,10,12)),
...     ]
>>> pprint.pprint(dedup(B))
[(('X', 'Y', 'Z', 0, 0, 0), ('A', 'B', 'C', 3, 3, 2)),
 (('J', 'K', 'L', 5, 4, 3), ('V', 'T', 'D', 5, 10, 12))]
于 2013-08-07T21:23:52.853 回答