Split an array in all possible combinations (not regular splitting)
Question:
Please read this question carefully before down voting. I could not find my problem in other questions here.
Suppose I have an array,
>>> import numpy as np
>>> array = np.linspace(1,4,4, dtype=np.int)
>>> array
array([1, 2, 3, 4])
I want a function that will split this array in all 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 is np.split(array, r)
, but it will not give all possible splits. e.g. np.split(array, 2)
will give,
[array([0, 1]), array([2, 3])]
As you can see this is not what I need. How to achieve my need?
Answers:
not familiar with numpy, but you can do it in pure python using divide and conquer(whether split on this position or not ):
def split(a):
if not a:
return [[]]
elif len(a) == 1:
return [[a]]
else:
result = []
for i in range(1, len(a) + 1):
result += [(a[:i], *sub_split) for sub_split in split(a[i:])]
return result
split([1,2,3])
# output => [([1], [2], [3]), ([1], [2, 3]), ([1, 2], [3]), ([1, 2, 3],)]
You could use itertools.combinations
to generate the indices where to split inside a loop over the number of splits:
>>> from itertools import combinations
>>> [np.split(array, idx)
... for n_splits in range(5)
... for 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])]]
Looking for a clue for the task, I came here. It’s my offer.
from more_itertools import partitions
def partitions_as_string(string):
return [part for part in partitions(string) if len(part) > 1]
print(partitions_as_string([1,2,3,4]))
Result:
[[[1], [2, 3, 4]], [[1, 2], [3, 4]], [[1, 2, 3], [4]], [[1], [2], [3, 4]], [[1], [2, 3], [4]], [[1, 2], [3], [4]], [[1], [2], [3], [4]]]
Please read this question carefully before down voting. I could not find my problem in other questions here.
Suppose I have an array,
>>> import numpy as np
>>> array = np.linspace(1,4,4, dtype=np.int)
>>> array
array([1, 2, 3, 4])
I want a function that will split this array in all 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 is np.split(array, r)
, but it will not give all possible splits. e.g. np.split(array, 2)
will give,
[array([0, 1]), array([2, 3])]
As you can see this is not what I need. How to achieve my need?
not familiar with numpy, but you can do it in pure python using divide and conquer(whether split on this position or not ):
def split(a):
if not a:
return [[]]
elif len(a) == 1:
return [[a]]
else:
result = []
for i in range(1, len(a) + 1):
result += [(a[:i], *sub_split) for sub_split in split(a[i:])]
return result
split([1,2,3])
# output => [([1], [2], [3]), ([1], [2, 3]), ([1, 2], [3]), ([1, 2, 3],)]
You could use itertools.combinations
to generate the indices where to split inside a loop over the number of splits:
>>> from itertools import combinations
>>> [np.split(array, idx)
... for n_splits in range(5)
... for 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])]]
Looking for a clue for the task, I came here. It’s my offer.
from more_itertools import partitions
def partitions_as_string(string):
return [part for part in partitions(string) if len(part) > 1]
print(partitions_as_string([1,2,3,4]))
Result:
[[[1], [2, 3, 4]], [[1, 2], [3, 4]], [[1, 2, 3], [4]], [[1], [2], [3, 4]], [[1], [2, 3], [4]], [[1, 2], [3], [4]], [[1], [2], [3], [4]]]