3

i have two list of list

a = [[1,2],[5,3],[7,9]]
b = [[2,4], [6,7]]

i would like to concatenate the list to

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

the goal is that if there are elements in the list that same, they will be concatenated. Any help is most appreciated.

sein
  • 47
  • 4

2 Answers2

4

This should work for the more general cases:

def connected_components(list_of_lists):
    """ based on Howard's answer https://stackoverflow.com/a/4842897/10693596 """
    temp_list_copy = list_of_lists.copy()
    result = []
    while len(temp_list_copy)>0:
        first, *rest = temp_list_copy
        first = set(first)

        lf = -1
        while len(first)>lf:
            lf = len(first)

            rest2 = []
            for r in rest:
                if len(first.intersection(set(r)))>0:
                    first |= set(r)
                else:
                    rest2.append(r)     
            rest = rest2
        result.append(list(first))
        temp_list_copy = rest
    return result


a = [[1,2],[5,3],[7,9]]
b = [[2,4], [6,7]]

a = connected_components(a)
b = connected_components(b)

for n, i in enumerate(a):
    combined_list = a[n]+ [jj for j in b if set(j).intersection(set(i)) for jj in j]

    a[n] = sorted(list(set(combined_list)))

print(a)

Or perhaps the following is a more pythonic version:


result = [
    sorted(
        set([
            k
            for j in b
            for k in (set(i)|set(j) if set(i)&set(j) else set(i))
        ])
    )
    for i in a
]
print(result)
SultanOrazbayev
  • 14,900
  • 3
  • 16
  • 46
  • 1
    This does not seems to work for second example in the comments :( It seems better to put "a" and "b" in only one list to work with. – Malo Mar 06 '21 at 10:24
  • I see, in general that's a much harder question, because you are essentially trying to find connected components in your original list `a`, so there are several solutions here: https://stackoverflow.com/questions/4842613/merge-lists-that-share-common-elements – SultanOrazbayev Mar 06 '21 at 10:30
  • I incorporated a function from the link to the answer. – SultanOrazbayev Mar 06 '21 at 10:49
  • Good! I updated my answer, with a shorter way to do it also. I will test yours. – Malo Mar 06 '21 at 10:51
  • both of those codes do not works if the cases are `a = [[1,2],[5,3],[7,9]]` and `b = [[2,4], [6,7], [9,3]]` – Moch. Chamdani M Mar 09 '21 at 08:59
  • Sure, if you want to connect the sublists, you can run `a = connected_components(a)` once more and I think it will give the desired result. – SultanOrazbayev Mar 09 '21 at 10:02
  • To elaborate, run the function on the resulting list to combine overlapping sublists – SultanOrazbayev Mar 09 '21 at 12:09
2

Here is a solution working with sets: It works with OP example and also example given in the comments.

a = [[1,2], [2,3],[7,9]]
b = [[2,4], [6,7]]     

a = a+b
b = []

while a != []:
    i = a.pop()
    for j in range(len(b)):
        if set(b[j]).intersection(set(i)) != set():
            b[j] = list(set(b[j]).union(set(i)))
            break
    else:
        if i != []:
            b.append(i)


print(b)
## [[9, 6, 7], [1, 2, 3, 4]]

Other tests:

a = [[8, 9], [1,2],[5,3],[7,9], [5, 6]]
b = [[2,4], [6,7]]
## [[3, 5, 6, 7, 8, 9], [1, 2, 4]]

a = [[1,2], [2,3],[7,9]]
b = [[2,4], [6,7]]
## [[9, 6, 7], [1, 2, 3, 4]]
Malo
  • 1,233
  • 1
  • 8
  • 25