How to remove duplicate from matrix ?
The Output I Want is :- [[-1,-,1,2],[-1,0,1]]
How to remove duplicate from matrix ?
The Output I Want is :- [[-1,-,1,2],[-1,0,1]]
list1 = [[-1, -1, 2], [-1, 0, 1], [-1, 2, -1],[0, 1,-1]]
print("The original list is : " + str(list1))
res = []
track = []
count = 0
for sub in list1:
res.append([]);
for ele in sub:
if ele not in track:
res[count].append(ele)
track.append(ele)
count += 1
print("The Matrix after duplicates removal is : " + str(res))
@dsedyoda Not getting the same output that I want [[-1,-,1,2],[-1,0,1]] the output I expect [[-1, 0, 1], [2], [], []] the output Iām getting
is it a matrix or list of lists??
(matrix is m*n i.e. all the lists are of same length while list of lists can contain lists of different length)
if it is a matrix, try this:
list1 = [[-1, -1, 0], [-1, 2, 1], [-1, 2, -1],[0, 1,-1]]
print("The original list is : " + str(list1))
res = []
track = [] # to track what elements are encountered
for i in list1:
if sorted(i) not in track:
track.append(sorted(i))
res.append(i)
print("The Matrix after duplicates removal is : " + str(res))
Try this solution
list1 = [[-1, -1, 2], [-1, 0, 1], [-1, 2, -1],[0, 1,-1]]
print("The original list is : " + str(list1))
res = []
track = [] # to track what elements are encountered
for i in list1:
if sorted(i) not in track:
track.append(sorted(i))
res.append(i)
print("The Matrix after duplicates removal is : " + str(res))
The output will be:
The original list is : [[-1, -1, 2], [-1, 0, 1], [-1, 2, -1], [0, 1, -1]]
The Matrix after duplicates removal is : [[-1, -1, 2], [-1, 0, 1]]
Output is same as you have expected