将数组分割成所有可能的组合(不是常规分割)

时间:2022-05-05 21:37:59

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 :

分割为len(数组)部分:

([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,

我知道有np。分割(数组,r),但它不会给出所有可能的分割。例如np。分裂(数组,2)会给,

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

As you can see this is not what I need. How to achieve my need?

正如你所看到的,这不是我所需要的。如何实现我的需求?

2 个解决方案

#1


3  

You could use itertools.combinations to generate the indices where to split inside a loop over the number of splits:

您可以使用itertools.combination来生成索引,在循环中对分割的数量进行分割:

>>> 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])]]

#2


2  

not familiar with numpy, but you can do it in pure python using divide and conquer(whether split on this position or not ):

不熟悉numpy,但是可以在纯python中使用divide and conquer(无论是否在这个位置上拆分):

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],)]

#1


3  

You could use itertools.combinations to generate the indices where to split inside a loop over the number of splits:

您可以使用itertools.combination来生成索引,在循环中对分割的数量进行分割:

>>> 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])]]

#2


2  

not familiar with numpy, but you can do it in pure python using divide and conquer(whether split on this position or not ):

不熟悉numpy,但是可以在纯python中使用divide and conquer(无论是否在这个位置上拆分):

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],)]