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!

7
  • 1
    This is not a full duplicate as the desired output is still a nested list. Commented Oct 12, 2021 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. Commented Oct 12, 2021 at 10:47
  • @DarrylG Was quick to judge. Reopened the question. Commented Oct 12, 2021 at 10:48
  • Of course it's possible. What have you tried? See How to Ask and how to create a minimal reproducible example. Commented Oct 12, 2021 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", ...)? Commented Oct 12, 2021 at 11:02

3 Answers 3

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']]

Sign up to request clarification or add additional context in comments.

2 Comments

Yes! This is exactly what I needed. I had a problem to make a thought about how the code is supposed to work.
Good luck @MatějKos
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']]

Comments

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

2 Comments

You only need the length parameter when you don't want the full length.
Yes, that's true. It's just that 9 times out of 10 I actually don't, so I'm used to specify it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.