-2

Given

x = [0,1,2,3]
y = [0,4,3,5]
z = [1,6,7,8]

I need something that removes all duplicates over all three lists randomly. So the desired outcome would be something like:

x = [1,2,3]
y = [0,4,5]
z = [6,7,8]

Running it another time should give a different distribution like for example:

x = [0,1,2,3]
y = [4,5]
z = [6,7,8]

All questions I've already found (like this one for example Remove both duplicates in multiple lists python) remove duplicates per list.

Thanks a lot in advance!

EDIT: Here is my current fruitless effort:

duplicatelist = []
    for number in y:
        if number in x:
            duplicatelist.append(number)
    for number in z:
        if number in x or number in y:
            duplicatelist.append(number)

#now I don*t know how to continue        
Difio
  • 145
  • 8

1 Answers1

1

If you start randomly (means x,y, z which one to treat first) you could get what you want.

import random
def rm_dup(**kwargs):
    inp = kwargs
    duplicate = set()
    keys = list(inp.keys())
    random.shuffle(keys)
    out = {k: [] for k in inp.keys()}

    for key in keys:
        for v in inp[key]:
            if v not in duplicate:
                out[key].append(v)
                duplicate.add(v)
    return out

example outs:

out = rm_dup(x=x,y=y,z=z)
print(out)

{'x': [2], 'y': [0, 4, 3, 5], 'z': [1, 6, 7, 8]}

{'x': [0, 1, 2, 3], 'y': [4, 5], 'z': [6, 7, 8]} ...

Thierry Lathuille
  • 23,663
  • 10
  • 44
  • 50
amirhm
  • 1,239
  • 9
  • 12