1

from permutations of array I have an array like this:

[['a', [['b', ['c']], ['c', ['b']]]], ['b', [['a', ['c']], ['c', ['a']]]], ['c', [['a', ['b']], ['b', ['a']]]]]

which means for 'a' I have 'b' and 'c' and for inner 'b' I have 'c' etc. I am confused how to make a new array which is representing this logic (not only for three variables) and I need it to look like this:

[['a','b','c'],['a','c','b'],['b','a','c'],['b','c','a'],['c','a','b'],['c','b','a']]

Is it somehow possible? Thank you!

Matěj Kos
  • 57
  • 5
  • 1
    This is not a full duplicate as the desired output is still a nested list. – Evgenii Oct 12 '21 at 10:45
  • @Ch3steR -- this is not a duplicate since the answers don't produce the desired output and it's non-trivial to modify these answers to produce the desired output. – DarrylG Oct 12 '21 at 10:47
  • @DarrylG Was quick to judge. Reopened the question. – Ch3steR Oct 12 '21 at 10:48
  • Of course it's possible. What have you tried? See [ask] and how to create a [mcve]. – Peter Wood Oct 12 '21 at 10:57
  • 1
    Is your first "array" (actually it is a nested list, not an array in Python terminology) to be taken as input, or can you simply start from the items to permutate ("a", "b", "c", ...)? – gimix Oct 12 '21 at 11:02
  • @gimix the task is to make an permutation of a list `['a','b','c',...]` and the first list what I have mentioned is the one I got from my own code, which gives sense to me but is not my desired ouput. – Matěj Kos Oct 12 '21 at 11:43
  • @MatějKos So you're saying this is a prime example of an [XY problem](https://xyproblem.info/)? – no comment Oct 12 '21 at 16:09

3 Answers3

2

You can write a recursive function in order to flatten the list.

def flatten(permutations):
    flattens = []
    for permutation in permutations:
        if len(permutation) == 2:
            flattens.extend([[permutation[0], *j] for j in flatten(permutation[1])])

        else:
            flattens.extend(permutation)

    return flattens


if __name__ == '__main__':
    permutations = [['a', [['b', ['c']], ['c', ['b']]]], ['b', [['a', ['c']], ['c', ['a']]]], ['c', [['a', ['b']], ['b', ['a']]]]]
    print(flatten(permutations))

Output:

[['a', 'b', 'c'], ['a', 'c', 'b'], ['b', 'a', 'c'], ['b', 'c', 'a'], ['c', 'a', 'b'], ['c', 'b', 'a']]

vikas soni
  • 540
  • 3
  • 9
1

Slightly shorter recursive solution with a generator:

data = [['a', [['b', ['c']], ['c', ['b']]]], ['b', [['a', ['c']], ['c', ['a']]]], ['c', [['a', ['b']], ['b', ['a']]]]]
def full_combos(d, c = []):
   if all(not isinstance(j, list) for j in d):
      yield from [c+[j] for j in d]
   else:
      yield from [j for a, b in d for j in full_combos(b, c+[a])]

print(list(full_combos(data)))

Output:

[['a', 'b', 'c'], ['a', 'c', 'b'], ['b', 'a', 'c'], ['b', 'c', 'a'], ['c', 'a', 'b'], ['c', 'b', 'a']]
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
0

The answer by vikas soni is great if you start from your first array. But if you start from a list of elements then the task is much simpler:

from itertools import permutations
ls = ["a", "b", "c"]
list(permutations(ls, len(ls)))
[('a', 'b', 'c'), ('a', 'c', 'b'), ('b', 'a', 'c'), ('b', 'c', 'a'), ('c', 'a', 'b'), ('c', 'b', 'a')]

ls2 = ["a", "b", "c", "d", "e"]
len(list(permutations(ls, len(ls))))
120
gimix
  • 3,431
  • 2
  • 5
  • 21