0

I have the following problem: I would like to find different "cuts" of the array into two different arrays by adding one element each time, for example:

If I have an array

a = [0,1,2,3]

The following splits are desired:

[0] [1,2,3]

[0,1] [2,3]

[0,1,2] [3]

In the past I had easier tasks so np.split() function was quite enough for me. How should I act in this particular case?

Many thanks in advance and apologies if this question was asked before.

Keithx
  • 2,994
  • 15
  • 42
  • 71
  • 1
    Does this answer your question? [Split an array in all possible combinations (not regular splitting)](https://stackoverflow.com/questions/45780190/split-an-array-in-all-possible-combinations-not-regular-splitting). (You can limit to `nsplits = 2`). – Pierre D Nov 25 '22 at 17:53

2 Answers2

2

Use slicing, more details : Understanding slicing.

a = [0,1,2,3]

for i in range(len(a)-1):
    print(a[:i+1], a[i+1:])

Output:

[0] [1, 2, 3]
[0, 1] [2, 3]
[0, 1, 2] [3]
I'mahdi
  • 23,382
  • 5
  • 22
  • 30
1

Check this out:

a = [0,1,2,3]

result = [(a[:x], a[x:]) for x in range(1, len(a))]

print(result)
# [([0], [1, 2, 3]), ([0, 1], [2, 3]), ([0, 1, 2], [3])]

# you can access result like normal list
print(result[0])
# ([0], [1, 2, 3])
Adrian Kurzeja
  • 797
  • 1
  • 11
  • 27