I use python to sequence some numbers. I would like to create a function that allows me to enter a value (4, 8, 16, 32, 64, etc.), create an array of numbers and change their sequence.
I added numbers that describe in detail how to determine the sequence for values = 4 and 8.
For value = 4, the array (x = [0, 1, 2, 3]) must be split into two ([0,1] and [2,3]) and then merged based on the first number in each array ([0 , 2, 1, 3]).

For value = 8, the array (x = [0, 1, 2, 3, 4, 5, 6, 7]) should be divided into two ([0, 1, 2, 3] and [4, 5, 6, 7] ) Both arrays should be divided into two ([0, 1, 2, 3] in [0,1] and [2,3] and [4, 5, 6, 7] in [4,5] and [6, 7] ) Then the arrays should be combined based on the first number in each array and the sequence of the second set of arrays ([0, 4, 2, 6, 1, 5, 3, 7]).

I don't know how to handle recursion (dynamically nested for loops). I try to go through each brek that is created by a split array. I looked at itertools and recursion ( Function with a different number of For Loops (python) loops ), but I could not get it to work. I have added code below to illustrate my approach so far.
Any help is greatly appreciated. I am also open to other ideas for sequencing.
I am using python 2.7.6 and numpy.
the code:
import numpy value = 4 a = [] x = numpy.arange(value) y = numpy.array_split(x, 2) for i in range(2): for j in y: a.append(j.tolist()[i]) print(a)
Output:
[0, 2, 1, 3]
the code:
import numpy value = 8 a = [] x = numpy.arange(value) y = numpy.array_split(x, 2) for i in range(2): for h in range(2): for j in y: z = numpy.array_split(j, 2) a.append(z[h][i]) print(a)
Output:
[0, 4, 2, 6, 1, 5, 3, 7]
The output for value = 16 should be [0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7 15].