mercredi 23 octobre 2019

permutations within all sublists

I wrote a function which should make permutations of elements within each sublist in nested list.

def swap(x):
    if isinstance(x, list):
        res = np.random.choice(x, len(x), replace = False)
        return [list(map(swap, res))]
    else:
        return x

So for example, if i use it with this:

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

It works and result is something like this

[['d', 'a', [['c', 'b']]]]
or 
[[[['c', 'b']], 'd', 'a']]

But when i use my function with this list:

x = ['a', [['b', [['c', ['e', 'f', 'g']]]], 'h', 'i', 'j']]

An error comes out and it says:

ValueError: x must be 1-dimensional

Why it doesn't work this this nested list?




Aucun commentaire:

Enregistrer un commentaire