【问题标题】:Merge lists with duplicated items which are withing one big list合并包含在一个大列表中的重复项目的列表
【发布时间】:2018-10-21 09:58:32
【问题描述】:

我需要一种有效的算法将具有重复项的列表合并到一个列表中。这些列表以不同的顺序具有相同的 excat 项目。它们都在一个大列表中。 示例:[ [1,2,3],[3,2,1],[2,1,3],[4,5],[5,4],[6] ] 输出应该是:[[1,2,3],[4,5],[6]]

我有这段代码,但是在迭代列表和删除项目时,我的索引超出了范围:

biglist = [ [1,2,3],[3,2,1],[2,1,3],[4,5],[5,4],[6] ]

for i in range(len(biglist)):
    temp = set(biglist[i])
    for j in range(i,len(biglist)-1):
        temp2 = set(biglist[j])
        if(temp == temp2):
            del biglist[j]

【问题讨论】:

  • 您需要按列表排序的顺序回答。
  • 您在循环遍历 biglist 时删除了项目,因此您的索引超出了范围。
  • @NiklasMertsch 我知道。我需要一个不同的解决方案

标签: python


【解决方案1】:

一种解决方案是对biglist 中的列表进行排序然后转换为元组, set() 将允许删除重复项。

def remove_dups(a):
    return list(map(list, set(map(tuple, map(sorted, a)))))

print (remove_dups(biglist))
# [[4, 5], [6], [1, 2, 3]]

【讨论】:

    【解决方案2】:

    解释在代码cmets中。

    试试这个:

    big_list = [ [1,2,3],[3,2,1],[2,1,3],[4,5],[5,4],[6] ]
    temp_list = []
    
    #for every small list in the big list
    for small_list in big_list:
        #sort small list
        small_list.sort()
        #if the small list is not in the temp list, add it
        if small_list not in temp_list:
            temp_list.append(small_list)
            #sort the temp list
            temp_list.sort()
    #print the temp list
    print (temp_list)
    

    输出:

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

    【讨论】:

      【解决方案3】:

      试试这个:

      import itertools
      biglist = [ [1,2,3],[3,2,1],[2,1,3],[4,5],[5,4],[6] ]
      sort_l = [sorted(i) for i in biglist]
      list(k for k,_ in itertools.groupby(sort_l))
      

      【讨论】:

        【解决方案4】:

        我修改了您自己的代码,而不是编写新代码。希望这可以帮助您了解问题所在并解决问题。

        biglist = [ [1,2,3],[3,2,1],[2,1,3],[4,5],[5,4],[6] ]
        
        #list to keep track of the indexes which should be deleted
        indexes=[]
        for i in range(len(biglist)):
            temp = set(biglist[i])
            for j in range(i+1,len(biglist)):
                temp2 = set(biglist[j])
                if(temp == temp2):
                    indexes.append(j)
        #to remove the  duplicates
        indexes=list(set(indexes))
        
        
        for i in range (len(indexes)):
            #to delete in reverse order so that indexes won't be affected
            del biglist[indexes[len(indexes)-i-1]]
        
        print biglist
        

        【讨论】:

        • 此代码不会涵盖重复项是 1 个元素的子列表的情况。尝试在列表中添加另一个 [6],您会看到它被包含两次。 (我现在不会拒绝你的答案,所以请尝试更正它)
        • j 的范围有问题。我的错。现在修好了。
        【解决方案5】:

        您可以将列表转换为已排序的tuples,然后在元组列表上使用set 并将它们转换回列表。

        lst = [i for i in set(tuple(sorted(i)) for i in biglist)]
        res = sorted([*i] for i in lst)
        
        print(res)
        # [[1, 2, 3], [4, 5], [6]]
        

        【讨论】:

        • 我在 [*i] 中的 * 处得到无效语法。如果我删除 * 我得到每个列表在元组内的输出
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多