2

I have a program which outputs solutions, which are lists of numbers as such:

    [[1, [[1, 5], [2, 4], [3, 3], [4, 2], [5, 1]]], [2, [[1, 4], [2, 3], [3, 2], [4, 1]]], [3, [[1, 3], [2, 2], [3, 1]]], [4, [[1, 2], [2, 1]]], [5, [[1, 1]]]]

What I want this to be turned into is:

    [[1,1,5],[1,2,4],[1,3,3],[1,4,2],[1,5,1],[2,1,4],[2,2,3],[2,3,2],[2,4,1],[3,1,3],[3,2,2],[3,3,1],[4,1,2],[4,2,1],[5,1,1]]

Basically, each term in the whole list is headed by a number, which forms the first item of all possible solutions with that number, and then the following lists indicate what is to be added.

Mrahta
  • 85
  • 6
  • 1
    Looks like an interesting question, but it isn't very well explained. "1 is part of all solutions" "6 starts a new solution", how is a 'solution' calculated? For example you have the following object in your output array "[6,7,8]", by what logic have you included 8 and skipped 9? – Parsa Jan 02 '16 at 15:31
  • 1
    So its a program to calculate the lists of numbers of a particular length that add up to a certain number. That example was a bit rubbish, I'm sorry. Changing it to a trial output from the program. – Mrahta Jan 02 '16 at 15:32

4 Answers4

0

This should work. It works with the example that you have atleast.

result = [[[i[0]] + j for j in i[1:][0] ] for i in arr][0]
mattsap
  • 3,790
  • 1
  • 15
  • 36
0

Introduce two functions: merge and flatten.

The second function is taken from here:

def flatten(lst):
    return [item for sublist in lst for item in sublist]

The first function is defined as:

def merge(lst):
    return [[lst[0]] + x for x in flatten(lst[1:])]

Then call them:

 s = [[1, [[1, 5], [2, 4], [3, 3], [4, 2], [5, 1]]], [2, [[1, 4], [2, 3], [3, 2], [4, 1]]], [3, [[1, 3], [2, 2], [3, 1]]], [4, [[1, 2], [2, 1]]], [5, [[1, 1]]]]
 print flatten([merge(x) for x in s])

Output is:

[[1, 1, 5], [1, 2, 4], [1, 3, 3], [1, 4, 2], [1, 5, 1], [2, 1, 4], [2, 2, 3], [2, 3, 2], [2, 4, 1], [3, 1, 3], [3, 2, 2], [3, 3, 1], [4, 1, 2], [4, 2, 1], [5, 1, 1]]
Community
  • 1
  • 1
kfx
  • 8,136
  • 3
  • 28
  • 52
0
items = [[1, [[1, 5], [2, 4], [3, 3], [4, 2], [5, 1]]], [2, [[1, 4], [2, 3], [3, 2], [4, 1]]], [3, [[1, 3], [2, 2], [3, 1]]], [4, [[1, 2], [2, 1]]], [5, [[1, 1]]]]
flat_items = []
for item in items:
    leading = item[0]
    for i in item[1]:
        flat_items.append([leading]+i)
print(flat_items)
kia
  • 828
  • 5
  • 12
  • This works here, but if the lists go deeper, so instead of that [1,5] there was something like [1,[2,3]], I would want it to output [[1,1,2],[1,1,3],... as in the code would have to do what yours does for the [1,[2,3]], but then using that do it again with the original 1, and this could go on indefinitely. – Mrahta Jan 02 '16 at 15:53
0

Yet another solution that gives you an output as you needed:

def flatten(lst):
    result = []
    for i in lst:
        for j in i[1]:
            pair = j[:]
            pair.insert(0, i[0])
            result.append(pair)
    return result
vrs
  • 1,922
  • 16
  • 23