0

I need to merge some part of lists using python, but still couldn't get working solution. For example, I have an arr list, and I want to get

[
   [1, 2, (3, 4, 5, 6, 7, 8, 9, 11, 1)], 
   [5, 6, (7, 8, 9, 10, 1)]
] 

So, it's very similar to sql query "select * from table where smth=smth1". Below there is my code which doesn't work well, though.

    arr = [[1, 2, 3, 4, 5, 6], [1, 2, 6, 7, 8, 9], [1, 2, 11, 1, 5, 9], [5, 6, 7, 8, 9, 10], [5, 6, 1, 1, 1, 1]]
    result = []
    for i in arr[:]:
        for j in arr[:]:
            if i[0] == j[0] and i[1] == j[1] and i != j:
                a = i[:]
                a[2:] = zip(i[2:], j[2:])
                result.append(a)
    
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • 4
    This question has been answered many times. – Marcin Mar 29 '12 at 12:25
  • 1
    What exactly does this mean? It is hard to understand... Do you want to flatten the list items? If so, there are many questions like this already. – jamylak Mar 29 '12 at 12:32
  • 1
    @Closevoters: note that the target structure is unusual, they want more than just flatten. – georg Mar 29 '12 at 12:34
  • Oh .. hmmm. The question is hard to understand, I suggest you reword it so it doesn't look like a duplicate :) – hochl Mar 29 '12 at 12:38
  • Oh i think i get it, he takes the first two elements of each list, puts them as the first two items in a list. Then he adds the rest of the list into a tuple. He repeats this, duplicate items are not added. Then he starts a new list as the first two items have changed to 5,6. – jamylak Mar 29 '12 at 12:45

2 Answers2

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

data = {}
for sub in arr:
    key = tuple(sub[:2])
    data[key] = data.get(key, set()).union(sub[2:])

print data

This produces something like:

{(1, 2): set([1, 3, 4, 5, 6, 7, 8, 9, 11]), (5, 6): set([8, 9, 10, 1, 7])}

It should be no problem to make your structure out of this.

georg
  • 211,518
  • 52
  • 313
  • 390
0

If the order of the resultant tuple does not matter, here is one solution using itertools.groupby()

from itertools import groupby

arr = [[1, 2, 3, 4, 5, 6], [1, 2, 6, 7, 8, 9], [1, 2, 11, 1, 5, 9], [5, 6, 7, 8, 9, 10], [5, 6, 1, 1, 1, 1]]
arr.sort()
print [[group[0][0], group[0][1], tuple(set(sum([sublist[2:] for sublist in group[1]], [])))] for group in groupby(arr, lambda l: l[0:2])]