Sometimes, while working with String lists, we can have a problem in which we need to perform the task of split and return all the split instances of list in cumulative way. This kind of problem can occur in many domains in which data is involved. Lets discuss certain ways in which this task can be performed.
Method #1 : Using loop
This is brute force way in which this task is performed. In this, we test for the list and append the list when ever we encounter the split character.
Python3
test_list = [ 'gfg-is-all-best' ]
print ( "The original list is : " + str (test_list))
spl_char = "-"
res = []
for sub in test_list:
for idx in range ( len (sub)):
if sub[idx] = = spl_char:
res.append([ sub[:idx] ])
res.append([ sub ])
print ( "The Cumulative List Splits : " + str (res))
|
OutputThe original list is : ['gfg-is-all-best']
The Cumulative List Splits : [['gfg'], ['gfg-is'], ['gfg-is-all'], ['gfg-is-all-best']]
Time Complexity: O(n*n) where n is the total number of values in the list “test_list”.
Auxiliary Space: O(n) where n is the total number of values in the list “test_list”.
Method #2 : Using accumulate() + join()
This is one-liner approach to this problem. In this, we perform the task of cutting into cumulative using accumulate and join() is used to construct the resultant List of lists.
Python3
from itertools import accumulate
test_list = [ 'gfg-is-all-best' ]
print ( "The original list is : " + str (test_list))
spl_char = "-"
temp = test_list[ 0 ].split(spl_char)
res = list (accumulate(temp, lambda x, y: spl_char.join([x, y])))
print ( "The Cumulative List Splits : " + str (res))
|
OutputThe original list is : ['gfg-is-all-best']
The Cumulative List Splits : ['gfg', 'gfg-is', 'gfg-is-all', 'gfg-is-all-best']
Time Complexity: O(n), where n is the length of the input list. This is because we’re using the accumulate() + join() which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list.
Method #3: Using recursion
Python3
def cumulative_list_split(lst, count):
result = []
sublist = []
for i, item in enumerate (lst):
sublist.append(item)
if (i + 1 ) % count = = 0 :
result.append(''.join(sublist))
sublist = []
if sublist:
result.append(''.join(sublist))
return result
my_string = 'gfg-is-all-best'
my_list = list (my_string)
result_list = cumulative_list_split(my_list, 4 )
print ( "The original list is :" , my_string)
print ( "The Cumulative List Splits :" , result_list)
|
OutputThe original list is : gfg-is-all-best
The Cumulative List Splits : ['gfg-', 'is-a', 'll-b', 'est']
Time complexity: O(n)
Space complexity: O(n)
Method #4: Using reduce():
Step-by-step approach:
- Initialize the list test_list and print it.
- Initialize the splitter character spl_char and print it.
- Using reduce() and split(), iterate through each split and concatenate them with spl_char using lambda function.
- Append the split strings to a new list and store it in res variable.
- Print the result list.
Python3
from functools import reduce
test_list = [ 'gfg-is-all-best' ]
print ( "The original list is : " + str (test_list))
spl_char = "-"
res = reduce ( lambda x, y: x + [spl_char.join(y)], [test_list[ 0 ].split(spl_char)[:i] for i in range ( 1 , len (test_list[ 0 ].split(spl_char)) + 1 )], [])
print ( "The Cumulative List Splits : " + str (res))
|
OutputThe original list is : ['gfg-is-all-best']
The Cumulative List Splits : ['gfg', 'gfg-is', 'gfg-is-all', 'gfg-is-all-best']
Time complexity: O(n^2), where n is the length of the input string, as we are using a nested for loop to generate all the possible substrings.
Auxiliary space: O(n^2), as we are storing all the possible substrings generated in a list. However, since the number of possible substrings is equal to the sum of the first n natural numbers, the worst-case space complexity can be approximated as O(n^2/2) = O(n^2).
Method #5: Using map() and list comprehension
In this method, we splits a string element in a list into its substrings using a specific character as a delimiter. The delimiter is stored in the variable spl_char. The list comprehension iterates over the string slices based on the split character using the join() method. The current slice is joined with the previous slices from 0 to i-1 (where i is the current iteration index), resulting in a cumulative split. All cumulative splits are stored in a list named res. Finally, the result is displayed using the print() function.
Python3
test_list = [ 'gfg-is-all-best' ]
print ( "The original list is : " + str (test_list))
spl_char = "-"
res = [spl_char.join(test_list[ 0 ].split(spl_char)[:i]) for i in range ( 1 , len (test_list[ 0 ].split(spl_char)) + 1 )]
print ( "The Cumulative List Splits : " + str (res))
|
OutputThe original list is : ['gfg-is-all-best']
The Cumulative List Splits : ['gfg', 'gfg-is', 'gfg-is-all', 'gfg-is-all-best']
Time complexity: O(n^2), where n is the length of the input string.
Auxiliary Space: O(n^2), as we are creating a list of length n(n+1)/2 to store all the possible substrings.