ATT
ATT

Reputation: 1081

Merge lists with duplicated items which are withing one big list

I need an efficient algorithm to merge lists with duplicated items into 1 list. the lists have the same excat items at different order. and they are all inside one big list. example: [ [1,2,3],[3,2,1],[2,1,3],[4,5],[5,4],[6] ] the output should be: [[1,2,3],[4,5],[6]]

i have this code but i get index out of range when iterating the list and removing items as i go:

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]

Upvotes: 1

Views: 146

Answers (5)

vash_the_stampede
vash_the_stampede

Reputation: 4606

You can convert the list to sorted tuplesthen use set on the list of tuples and convert them back to lists.

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]]

Upvotes: 0

Tyesh
Tyesh

Reputation: 380

I modified your own code instead of writing a new one. Hope this might help you to understand what went wrong and solve it.

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

Upvotes: 0

Rohit-Pandey
Rohit-Pandey

Reputation: 2159

Try This:

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))

Upvotes: 0

Thaer A
Thaer A

Reputation: 2323

Explanation is in the code comments.

Try this:

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)

Output:

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

Upvotes: 1

Ghilas BELHADJ
Ghilas BELHADJ

Reputation: 14096

One solution is to sort then convert to tuples the lists inside biglist, set() will then allow to remove duplicates.

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

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

Upvotes: 1

Related Questions