1

Suppose I have an list of lists:

lists = [
    [1, 2, 5, 7],
    [3, 6, 8, 10],
    [2, 7, 9, 11]
]

I want to merge them (lowest to greatest index) if they have an intersecting value, while not copying the duplicate(s), so that the output looks like:

new_lists = [
    [1, 2, 5, 7, 9, 11],
    [3, 6, 8, 10],
]

I how can I manage this for large amount of lists, while being relatively eficient?

hhhhhhhhhn
  • 31
  • 3
  • What happened to 11? – fyngyrz Jul 04 '19 at 20:34
  • search for intersection of 2 lists (using set methods) and build up from there. one point i'm not clear on is the order of the intersected values – Chris_Rands Jul 04 '19 at 20:41
  • look at https://stackoverflow.com/questions/3697432/how-to-find-list-intersection/33067553 – Bob White Jul 04 '19 at 20:51
  • okay, so you just missed 11. Thanks for the edit. Next question: you had three lists. Now you have two. It's non-obvious to me the criteria you used to fill the two outputs when merging. Why not one? You really need to explain *exactly* what it is you are trying to do here. :) – fyngyrz Jul 04 '19 at 20:58
  • Isn't the index of 9 = 2 and the index of 7 = 3, so should 9 be before 7 in the result? – DaveStSomeWhere Jul 04 '19 at 21:11
  • @fyngyrz could you check my answer please – Ghassen Jul 04 '19 at 21:18
  • Why not one output list with no dupes? That's trivial to do. You could even break it into multiple lists on finish if you wanted. Push all the values into a dictionary; dupes will overwrite themselves. Then take the key list of the dict (which will come to you as a list) and sort it if you like, and split it if you like. Would that do what you want? – fyngyrz Jul 04 '19 at 21:40

2 Answers2

1

So here's a Python 2.7 approach similar to my comment above:

lists = [
    [1, 2, 5, 7, 13],
    [3, 6, 8, 10, 13],
    [2, 7, 9, 11]
]

thedict = {}
for sublist in lists:
    for el in sublist:
        thedict[el] = 1

keylist = thedict.keys()

# at this point, you have one merged list:

print str(keylist)

# You might want to sort it, as key order is not assured

keylist.sort()

# if you want smaller lists, then you can do something like this:

listsize = 5
outlistoflists = []
for i in range(0, len(keylist), listsize):
    sublist = keylist[i:i + listsize]
    outlistoflists.append(sublist)

print str(outlistoflists)
fyngyrz
  • 2,458
  • 2
  • 36
  • 43
0

try this:

lists = [
    [1, 2, 5, 7],
    [3, 6, 8, 10],
    [2, 7, 9, 11]
]

newlist=[]
for sublist in lists:
    r=sublist[-1]
    i=1
    for sublist_ in lists[i:]:
        if r in sublist_:
            lists.remove(sublist_)
            sublist.extend(sublist_[sublist_.index(r)+1:])
            newlist.append(sublist)
    i=i+1
print([list(i) for i in set(map(tuple, newlist))])

output

[[1, 2, 5, 7, 9, 11], [3, 6, 8, 10]]
Ghassen
  • 764
  • 6
  • 14
  • For `lists = [ [1, 2, 5, 7], [3, 6, 8, 10], [2, 7, 9, 11], [2, 7, 9, 11, 23] ]` Returns `[[1, 2, 5, 7, 9, 11, 9, 11, 23], [1, 2, 5, 7, 9, 11, 9, 11, 23], [3, 6, 8, 10]]` is that valid? – DaveStSomeWhere Jul 04 '19 at 21:07
  • @DaveStSomeWhere try it now the result should be [[3, 6, 8, 10], [1, 2, 5, 7, 9, 11, 9, 11, 23]] – Ghassen Jul 04 '19 at 21:13
  • Now you just need to remove the dups. Also, don't you need parenthesis for the final `print()` – DaveStSomeWhere Jul 04 '19 at 21:19
  • i'm using python 2.7.2 the print work with and without () but i will add it – Ghassen Jul 04 '19 at 21:20