python - Split an array in all possible combinations (not regular splitting) -


please read question before down voting. not find problem in other questions here.

suppose have array,

>>> import numpy np >>> array  = np.linspace(1,4,4, dtype=np.int) >>> array array([1, 2, 3, 4]) 

i want function split array in possible parts, such that,

no split :

([1,2,3,4]) 

split in 2 parts :

([1], [2,3,4]) ([1,2], [3,4]) ([1,2,3] ,[4]) 

split in 3 parts :

([1], [2], [3,4]) ([1,2]), [3], [4]) ([1], [2,3], [4]) 

split in len(array) parts :

([1],[2],[3],[4]) 

i know there np.split(array, r), not give possible splits. e.g. np.split(array, 2) give,

[array([0, 1]), array([2, 3])] 

as can see not need. how achieve need?

you use itertools.combinations generate indices split inside loop on number of splits:

>>> itertools import combinations >>> [np.split(array, idx)  ...  n_splits in range(5)  ...  idx in combinations(range(1, len(array)), n_splits)] [[array([1, 2, 3, 4])],  [array([1]), array([2, 3, 4])],  [array([1, 2]), array([3, 4])],  [array([1, 2, 3]), array([4])],  [array([1]), array([2]), array([3, 4])],  [array([1]), array([2, 3]), array([4])],  [array([1, 2]), array([3]), array([4])],  [array([1]), array([2]), array([3]), array([4])]] 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -