Home > Mobile >  Python: Remove values that are duplicated in the previous list inside a nested list
Python: Remove values that are duplicated in the previous list inside a nested list

Time:08-24

Example of a given nested list:

nList = [[2,5,99,99],[-3,8,1,2,10],[1, 7,100,10]]

Expectations: Remove values that are duplicated in the previous list.

Expected Output:

oList = [[2, 5, 99], [-3, 8, 1, 10], [7, 100]]

My Codes:

def RemoveDup(nList):
    lst = []
    for i in nList:
        for j in i:
            if j not in lst:
                lst.append(j)            
    return lst 

>>> print(RemoveDup([[2,5,99,99],[-3,8,1,2,10],[1, 7,100,10]]))
>>> [2, 5, 99, -3, 8, 1, 10, 7, 100]

I still couldn't figure out how to make the output a nested list like the expected output, any help and advice are appreciated!

CodePudding user response:

As there is no "previous" element to compare against the first element in the nested list then it must be dealt with as is.

This may achieve the real objective:

nList = [[2,5,99,99],[-3,8,1,2,10],[1, 7,100,10]]

output = [nList[0]]

for e in nList[1:]:
    t = []
    for x in e:
        if x not in output[-1]:
            t.append(x)
    output.append(t)

print(output)

Output:

[[2, 5, 99, 99], [-3, 8, 1, 10], [7, 100]]

If duplicates need to be removed from the first element in the list then:

nList = [[2,5,99,99],[-3,8,1,2,10],[1, 7,100,10]]

output = []

t = []

for e in nList[0]:
    if e not in t:
        t.append(e)
output.append(t)
for e in nList[1:]:
    t = []
    for x in e:
        if x not in output[-1]:
            t.append(x)
    output.append(t)

print(output)

Output:

[[2, 5, 99], [-3, 8, 1, 10], [7, 100]]

Note:

The temptation to use sets should be avoided as the order of the output list may not be as expected

CodePudding user response:

Using this solution you don't even need to make an auxiliary list. It deletes the duplicates in place while the function iterates through the numbers.

This is a much more efficient as it saves the time of building another data structure and saves the space necessary for filling up another nested list for the solution.

nList = [[2,5,99,99],[-3,8,1,2,10],[1, 7,100,10]]

def RemoveDup(nList):
    j = len(nList) - 1          # size of list
    while j >= 0:               # iterate through sublists
        k = len(nList[j]) - 1   # size of sublist
        while k >= 0:           # iterate through each number
            if j - 1 < 0:       # check if there are any lists before position

                # if not then check the current list for duplicates
                if nList[j][k] in nList[j][:k]:  
                    del nList[j][k]  # if there is a duplicate delete it
            
            # if there is a list before the current position check if 
            # it contains the current number
            elif nList[j][k] in nList[j-1]:   
                del nList[j][k]   # if it does delete it
            k -= 1
        j -= 1

RemoveDup(nList)
print(nList)

OUTPUT

[[2, 5, 99], [-3, 8, 1, 10], [7, 100]]

CodePudding user response:

Code:

def RemoveDup(nList):
    tmp =  list(chain.from_iterable(nList))             #Converting nested list to list
    dup = list(set([x for x in tmp if tmp.count(x) > 1]))   #finding the duplicates

    for i in reversed(nList):        #we have reverse the loop so 
          for j in i:                # we can keep first and remove second duplicate
            if j in dup:
                dup.remove(j)        #emptying the dup list as 
                i.remove(j)          # we are deleting the duplicate values
    return nList


RemoveDup(nList)

Output:

[[2, 5, 99], [-3, 8, 1, 10], [7, 100]]

CodePudding user response:

You need to add two intermediate containers

nList = [[2, 5, 99, 99], [-3, 8, 1, 2, 10], [1, 7, 100, 10]]

def RemoveDup(nList):
    aggregation = []
    vessel2 = []
    for i in nList:
        lst = []
        for j in i:
            if j not in aggregation:
                lst.append(j)
                aggregation.append(j)
        vessel2.append(lst)
    return vessel2

print(RemoveDup(nList))

  • Related