1

I have a list x = [['a', 'b', 'c'], ['d'], ['e', 'f', ['g', ['h', 'i']]]] I want to do all possible permutations of elements in sublists without going beyond corresponding sublist. I use this generator:

import numpy as np
def ff(x):
    if isinstance(x, list) == True:
        res = np.random.choice(x, len(x), replace = False)
        return [list(map(ff, res))]

    else:
        print(x)

But however when i do

ff(x)

I get something like this:

[[[[None]], [[None, None, None]], [[[[None, [[None, None]]]], None, None]]]]

Could you tell me please where i made a mistake? The expected output are variations of something like this:

[['c', 'b', 'a'], ['d'], ['f', 'e', ['g', ['i', 'h']]]]
[['d'], ['a', 'b', 'c'], ['f', 'e', [['h', 'i'], 'g']]]

As you see each element is kept in it's square bracket. I need it for tree leafs permutations.

  • 4
    You want to `return x`, not `print(x)`. – sanyassh Oct 08 '19 at 14:16
  • Can you update your question to include expected output? If you have to, show us expected output for a simpler input. This will help clarify your question and get a quick answer :D – wgb22 Oct 08 '19 at 14:16
  • @sanyash yeah that's it, thanks, im so inattentive sometimes) –  Oct 08 '19 at 14:23
  • 1
    Just a suggestion: if isinstance(x, list): is sufficient, == True is not needed and generally not recommended – micric Oct 08 '19 at 14:29

0 Answers0