Search code examples
python-3.xchunks

How can I have a python function that effectively divide a list to all possible number of chunks?


I want to split a list (float or integer) according to the following conditions:

  • Splitting the list into all possible subsamples.
  • No duplication.
  • A unit sample cannot be a subsample.

I have what splits a list into equal sizes by giving the number of subsamples.

The code I have laid my hand on which worked but does not give me what I want

import numpy as np
x = [1,2,3,4,5,6,7,8,9,10]
l = np.array_split(x,3)

output

[[1, 2, 3, 4], [5, 6, 7], [8, 9, 10]]

I desire to have a list of subsample of all possibilities without duplication. that is, a combination of all unique possibilities when the list is split into 2, 3, 4, etc (no two or more subsamples will have the same element)).

I do not what to specify chunk number so that it will not be limited to such number.

Here is what I did manually

From the following series [1,2,3,4,5,6,7,8,9,10] I sliced it into all possible blocks as follows:

when splitting into 2

[1][2,3,4,5,6,7,8,9,10]
[1,2][3,4,5,6,7,8,9,10]
[1,2,3][4,5,6,7,8,9,10]
[1,2,3,4][5,6,7,8,9,10]
[1,2,3,4,5][6,7,8,9,10]
[1,2,3,4,5,6][7,8,9,10]
[1,2,3,4,5,6,7,8][9,10]
[1,2,3,4,5,6,7,8,9][10]

when splitting into 3

[1][2][3,4,5,6,7,8,9,10]
[1][2,3][4,5,6,7,8,9,10]
[1][2,3,4][5,6,7,8,9,10]
[1][2,3,4,5][6,7,8,9,10]
[1][2,3,4,5,6][7,8,9,10]
[1][2,3,4,5,6,7][8,9,10]
[1][2,3,4,5,6,7,8][9,10]
[1][2,3,4,5,6,7,8,9][10]

[1,2][3][4,5,6,7,8,9,10]
[1,2][3,4][5,6,7,8,9,10]
[1,2][3,4,5][6,7,8,9,10]
[1,2][3,4,5,6][7,8,9,10]
[1,2][3,4,5,6,7][8,9,10]
[1,2][3,4,5,6,7,8][9,10]
[1,2][3,4,5,6,7,8,9][10]

[1,2,3][4][5,6,7,8,9,10]
[1,2,3][4,5][6,7,8,9,10]
[1,2,3][4,5,6][7,8,9,10]
[1,2,3][4,5,6,7][8,9,10]
[1,2,3][4,5,6,7,8][9,10]
[1,2,3][4,5,6,7,8,9][10]

[1,2,3,4][5][6,7,8,9,10]
[1,2,3,4][5,6][7,8,9,10]
[1,2,3,4][5,6,7][8,9,10]
[1,2,3,4][5,6,7,8][9,10]
[1,2,3,4][5,6,7,8,9][10]

[1,2,3,4,5][6][7,8,9,10]
[1,2,3,4,5][6,7][8,9,10]
[1,2,3,4,5][6,7,8][9,10]
[1,2,3,4,5][6,7,8,9][10]

[1,2,3,4,5,6][7][8,9,10]
[1,2,3,4,5,6][7,8][9,10]
[1,2,3,4,5,6][7,8,9][10]

when splitting into 4

[1,2,3,4,5,6,7][8][9,10]
[1,2,3,4,5,6,7][8,9][10]

[1,2,3,4,5,6,7,8][9][10]

After all possible splitting into blocks, I removed all the single digit blocks and also removed all duplicated blocks.

[2,3,4,5,6,7,8,9,10]
[1,2,3,4,5,6,7,8,9]


[3,4,5,6,7,8,9,10]
[2,3]
[2,3,4]
[2,3,4,5]
[2,3,4,5,6]
[2,3,4,5,6,7]
[2,3,4,5,6,7,8]
[2,3,4,5,6,7,8,9]

[4,5,6,7,8,9,10]
[3,4]
[3,4,5]
[3,4,5,6]
[3,4,5,6,7]
[3,4,5,6,7,8]
[1,2][3,4,5,6,7,8,9]

[5,6,7,8,9,10]
[4,5]
[4,5,6]
[4,5,6,7]
[4,5,6,7,8]
[1,2,3][4,5,6,7,8,9]

[6,7,8,9,10]
[5,6]
[5,6,7]
[5,6,7,8]
[1,2,3,4][5,6,7,8,9]

[1,2,3,4,5][7,8,9,10]
[6,7]
[6,7,8]
[6,7,8,9]

[8,9,10]
[7,8]
[1,2,3,4,5,6][7,8,9]


[9,10]
[1,2,3,4,5,6,7][8,9]

[1,2,3,4,5,6,7,8]

Here, I gather all the possible chunks together.

This is what I desire as an output.

[[2,3,4,5,6,7,8,9,10], [1,2,3,4,5,6,7,8,9], [3,4,5,6,7,8,9,10], [2,3], [2,3,4], [2,3,4,5], [2,3,4,5,6], [2,3,4,5,6,7], [2,3,4,5,6,7,8], [2,3,4,5,6,7,8,9], [4,5,6,7,8,9,10],[3,4], [3,4,5], [3,4,5,6], [3,4,5,6,7], [3,4,5,6,7,8], [1,2], [3,4,5,6,7,8,9], [5,6,7,8,9,10], [4,5], [4,5,6], [4,5,6,7], [4,5,6,7,8], [1,2,3], [4,5,6,7,8,9], [6,7,8,9,10], [5,6], [5,6,7], [5,6,7,8], [1,2,3,4], [5,6,7,8,9], [1,2,3,4,5], [7,8,9,10], [6,7], [6,7,8], [6,7,8,9], [8,9,10], [7,8], [1,2,3,4,5,6], [7,8,9], [9,10], [1,2,3,4,5,6,7], [8,9], [1,2,3,4,5,6,7,8]]

Solution

  • It looks to me like the problem reduces to finding all substrings of length 2 or greater that leave at least one fragment of length 1. In other words, you won't have to enumerate every partition to find them.

    def parts(thing):
        result = []
        for i in range(len(thing)):
            for j in range(i + 1, len(thing) + 1):
                if 1 < len(thing[i:j]) < len(thing):
                    result.append(thing[i:j])
        return result
    
    res = parts([*range(1,11)])
    # res
    # [[1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5, 6],
    # [1, 2, 3, 4, 5, 6, 7], [1, 2, 3, 4, 5, 6, 7, 8],
    # [1, 2, 3, 4, 5, 6, 7, 8, 9], [2, 3], [2, 3, 4], [2, 3, 4, 5],
    # [2, 3, 4, 5, 6], [2, 3, 4, 5, 6, 7], [2, 3, 4, 5, 6, 7, 8],
    # [2, 3, 4, 5, 6, 7, 8, 9], [2, 3, 4, 5, 6, 7, 8, 9, 10], [3, 4], [3, 4, 5],
    # [3, 4, 5, 6], [3, 4, 5, 6, 7], [3, 4, 5, 6, 7, 8], [3, 4, 5, 6, 7, 8, 9],
    # [3, 4, 5, 6, 7, 8, 9, 10], [4, 5], [4, 5, 6], [4, 5, 6, 7], [4, 5, 6, 7, 8],
    # [4, 5, 6, 7, 8, 9], [4, 5, 6, 7, 8, 9, 10], [5, 6], [5, 6, 7], [5, 6, 7, 8],
    # [5, 6, 7, 8, 9], [5, 6, 7, 8, 9, 10], [6, 7], [6, 7, 8], [6, 7, 8, 9],
    # [6, 7, 8, 9, 10], [7, 8], [7, 8, 9], [7, 8, 9, 10], [8, 9], [8, 9, 10],
    # [9, 10]]