代码之家  ›  专栏  ›  技术社区  ›  aviss

使用python从不同长度的元组列表中删除重复项

  •  3
  • aviss  · 技术社区  · 7 年前

    我使用regex等从文本中提取特定的名称。结果是包含标题和名称的元组列表。元组的长度可能不同。 lst 下面列出了可能的情况。我需要从结果中删除重复的名称。例如,('lord'、'justice')==('lord'、'justice'、'smith')和('lady'、'smits')==('lady'、'justice'、'smitles'),但是('lord'、'justice'、'smith')和('lady'、'justice'、'smits')是不同的名称。中每个元素的所需输出 LST 应该是 [('Lord', 'Justice', 'Smith'), ('Lady', 'Justice', 'Smiles')]

    lst = [[('Lord', 'Justice', 'Smith'), ('Lady', 'Justice', 'Smiles')],
           [('Lord', 'Justice', 'Smith'), ('Lady', 'Justice', 'Smiles'), ('Lord', 'Justice')],
           [('Lord', 'Justice', 'Smith'), ('Lady', 'Smiles'), ('Lady', 'Justice', 'Smiles')],
           [('Lord', 'Justice', 'Smith'), ('Lady', 'Justice'), ('Lady', 'Justice', 'Smiles')],
           [('Lord', 'Justice', 'Smith'), ('Lady', 'Justice', 'Smiles'), ('Lady', 'Smiles')]]
    

    这是我现在拥有的,但它不能产生期望的输出。非常感谢您的帮助和建议。

    for l in lst:
        print(l)
        # remove duplicates based on the last index in tuples
        lst_1 = list(dict((v[-1],v) for v in sorted(l, key=lambda l: lst[0])).values())
        print(lst_1)
        # remove duplicates based on the second index [1] in tuples
        lst_2 = list(dict((v[1],v) for v in sorted(lst_1, key=lambda lst_1: lst_1[0])).values())    
        print(lst_2)
        print("\n")
    

    更新:

    我的例子可能太具体了。我必须包括其他名称,因此当存在其他名称时,解决方案应该有效:

    lst = [
    [('Lord', 'Justice', 'Smith'), ('Lady', 'Justice', 'Smiles'), ('Lord', 'Other'), ('Lady', 'Another'), ('Lady', 'Diana', 'Spencer'), ('Lord', 'Dave', 'Castle')],
    [('Lord', 'Justice', 'Smith'), ('Lady', 'Justice', 'Smiles'), ('Lord', 'Justice'), ('Lord', 'Other'), ('Lady', 'Another'), ('Lady', 'Diana', 'Spencer'), ('Lord', 'Dave', 'Castle')],
    [('Lord', 'Justice', 'Smith'), ('Lady', 'Smiles'), ('Lady', 'Justice', 'Smiles'), ('Lord', 'Other'), ('Lady', 'Another'), ('Lady', 'Diana', 'Spencer'), ('Lord', 'Dave', 'Castle')],
    [('Lord', 'Justice', 'Smith'), ('Lady', 'Justice'), ('Lady', 'Justice', 'Smiles'), ('Lord', 'Other'), ('Lady', 'Another'), ('Lady', 'Diana', 'Spencer'), ('Lord', 'Dave', 'Castle')],
    [('Lord', 'Justice', 'Smith'), ('Lady', 'Justice', 'Smiles'), ('Lady', 'Smiles'), ('Lord', 'Other'), ('Lady', 'Another'), ('Lady', 'Diana', 'Spencer'), ('Lord', 'Dave', 'Castle')]
    ]
    

    理想输出:

    [('Lord', 'Justice', 'Smith'), ('Lady', 'Justice', 'Smiles'), ('Lord', 'Other'), ('Lady', 'Another'), ('Lady', 'Diana', 'Spencer'), ('Lord', 'Dave', 'Castle')]
    [('Lord', 'Justice', 'Smith'), ('Lady', 'Justice', 'Smiles'), ('Lord', 'Other'), ('Lady', 'Another'), ('Lady', 'Diana', 'Spencer'), ('Lord', 'Dave', 'Castle')]
    [('Lord', 'Justice', 'Smith'), ('Lady', 'Justice', 'Smiles'), ('Lord', 'Other'), ('Lady', 'Another'), ('Lady', 'Diana', 'Spencer'), ('Lord', 'Dave', 'Castle')]
    [('Lord', 'Justice', 'Smith'), ('Lady', 'Justice', 'Smiles'), ('Lord', 'Other'), ('Lady', 'Another'), ('Lady', 'Diana', 'Spencer'), ('Lord', 'Dave', 'Castle')]
    [('Lord', 'Justice', 'Smith'), ('Lady', 'Justice', 'Smiles'), ('Lord', 'Other'), ('Lady', 'Another'), ('Lady', 'Diana', 'Spencer'), ('Lord', 'Dave', 'Castle')]
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Sunitha    7 年前

    你可以用 itertools.groupby

    lst = [
    [('Lord', 'Justice', 'Smith'), ('Lady', 'Justice', 'Smiles'), ('Lord', 'Other'), ('Lady', 'Another'), ('Lady', 'Diana', 'Spencer'), ('Lord', 'Dave', 'Castle')],
    [('Lord', 'Justice', 'Smith'), ('Lady', 'Justice', 'Smiles'), ('Lord', 'Justice'), ('Lord', 'Other'), ('Lady', 'Another'), ('Lady', 'Diana', 'Spencer'), ('Lord', 'Dave', 'Castle')],
    [('Lord', 'Justice', 'Smith'), ('Lady', 'Smiles'), ('Lady', 'Justice', 'Smiles'), ('Lord', 'Other'), ('Lady', 'Another'), ('Lady', 'Diana', 'Spencer'), ('Lord', 'Dave', 'Castle')],
    [('Lord', 'Justice', 'Smith'), ('Lady', 'Justice'), ('Lady', 'Justice', 'Smiles'), ('Lord', 'Other'), ('Lady', 'Another'), ('Lady', 'Diana', 'Spencer'), ('Lord', 'Dave', 'Castle')],
    [('Lord', 'Justice', 'Smith'), ('Lady', 'Justice', 'Smiles'), ('Lady', 'Smiles'), ('Lord', 'Other'), ('Lady', 'Another'), ('Lady', 'Diana', 'Spencer'), ('Lord', 'Dave', 'Castle')]
    ]
    res = [[max(reversed(list(v)), key=len) for k,v in groupby(sl, lambda x: x[0])] for sl in lst]
    for l in res:
        print(l)
    

    产量

    [('Lord', 'Justice', 'Smith'), ('Lady', 'Justice', 'Smiles'), ('Lord', 'Other'), ('Lady', 'Diana', 'Spencer'), ('Lord', 'Dave', 'Castle')]
    [('Lord', 'Justice', 'Smith'), ('Lady', 'Justice', 'Smiles'), ('Lord', 'Other'), ('Lady', 'Diana', 'Spencer'), ('Lord', 'Dave', 'Castle')]
    [('Lord', 'Justice', 'Smith'), ('Lady', 'Justice', 'Smiles'), ('Lord', 'Other'), ('Lady', 'Diana', 'Spencer'), ('Lord', 'Dave', 'Castle')]
    [('Lord', 'Justice', 'Smith'), ('Lady', 'Justice', 'Smiles'), ('Lord', 'Other'), ('Lady', 'Diana', 'Spencer'), ('Lord', 'Dave', 'Castle')]
    [('Lord', 'Justice', 'Smith'), ('Lady', 'Justice', 'Smiles'), ('Lord', 'Other'), ('Lady', 'Diana', 'Spencer'), ('Lord', 'Dave', 'Castle')]
    
        2
  •  1
  •   Andrej Kesely    7 年前

    我带来了这个解决方案:

    from itertools import chain, groupby
    
    lst = [
    [('Lord', 'Justice', 'Smith'), ('Lady', 'Justice', 'Smiles')],
    [('Lord', 'Justice', 'Smith'), ('Lady', 'Justice', 'Smiles'), ('Lord', 'Justice')],
    [('Lord', 'Justice', 'Smith'), ('Lady', 'Smiles'), ('Lady', 'Justice', 'Smiles')],
    [('Lord', 'Justice', 'Smith'), ('Lady', 'Justice'), ('Lady', 'Justice', 'Smiles')],
    [('Lord', 'Justice', 'Smith'), ('Lady', 'Justice', 'Smiles'), ('Lady', 'Smiles')]
    ]
    
    def remove_duplicates(lst):
        rv = []
        for g, v in groupby([g for g, _ in groupby(sorted(lst))], key=lambda v: v[0]):
            rv.append(max(list(v), key=lambda v: len(v)))
        return rv
    
    
    for option in lst:
        print(remove_duplicates(option))
    

    输出:

    [('Lady', 'Justice', 'Smiles'), ('Lord', 'Justice', 'Smith')]
    [('Lady', 'Justice', 'Smiles'), ('Lord', 'Justice', 'Smith')]
    [('Lady', 'Justice', 'Smiles'), ('Lord', 'Justice', 'Smith')]
    [('Lady', 'Justice', 'Smiles'), ('Lord', 'Justice', 'Smith')]
    [('Lady', 'Justice', 'Smiles'), ('Lord', 'Justice', 'Smith')]
    
    推荐文章