#This function will produce sublist of length 3 by generating the cut points for a list of n.
def cut_points(n, already_cut=None):
# The first cut point is at 0
if already_cut is None:
already_cut = [0]
# We can cut at all places between the last cut plus 3
# and the length minus 3, and yield recursively the solutions for each choice
for i in range(already_cut[-1]+3, n-2):
cuts = already_cut[:] + [i]
yield from cut_points(n, cuts)
# When we tried all cut points and reached the total length, we yield the cut points list
yield already_cut[:] + [n]
#This provides the sublists
def all_possible_sublists(data):
n = len(data)
for cut in cut_points(n):
yield [data[cut[i]:cut[i+1]] for i in range(len(cut)-1)]
list(all_possible_sublists([0, 1, 2, 3]))
# [[[0, 1, 2, 3]]]
list(all_possible_sublists([0, 1, 2, 3, 4, 5, 6]))
# [[[0, 1, 2], [3, 4, 5, 6]],
# [[0, 1, 2, 3], [4, 5, 6]],
# [[0, 1, 2, 3, 4, 5, 6]]]
for sublist in all_possible_sublists([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]):
print(sublist)
# [[0, 1, 2], [3, 4, 5], [6, 7, 8, 9]]
# [[0, 1, 2], [3, 4, 5, 6], [7, 8, 9]]
# [[0, 1, 2], [3, 4, 5, 6, 7, 8, 9]]
# [[0, 1, 2, 3], [4, 5, 6], [7, 8, 9]]
# [[0, 1, 2, 3], [4, 5, 6, 7, 8, 9]]
# [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]
# [[0, 1, 2, 3, 4, 5], [6, 7, 8, 9]]
# [[0, 1, 2, 3, 4, 5, 6], [7, 8, 9]]
# [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]
If you wish to learn more about how to use python for data science, then go through data science python programming course by Intellipaat for more insights.