Python – Synchronized Split list with other
Last Updated :
03 May, 2023
Sometimes, while working with Python data, we can have a problem in which we need to perform split of a String, which has mapping with an element on other lists, so need to perform mapping to different words formed due to the split. This type of problem is peculiar but can have applications in many domains. Let’s discuss certain ways in which this task can be performed.
Input : test_list = [5, 6], str_list = [‘Gfg is best’, ‘I love Gfg’]
Output : [5, 5, 5, 6, 6, 6]
Input : test_list = [5], str_list = [‘Gfg is best’]
Output : [5, 5, 5]
Method #1 : Using map() + zip() + enumerate() + split() The combination of above functions can be used to solve this problem. In this, we perform the task of splitting values using split(), and corresponding element mapping using zip() and map(). The iteration is carried using enumerate().
Python3
test_list = [ 5 , 6 , 3 , 7 , 4 ]
print ("The original list is : " + str (test_list))
str_list = [ 'Gfg' , 'is best' , 'I love' , 'Gfg' , 'and CS' ]
temp = list ( map ( list , zip ( * [(idx, sub) for idx, val in
enumerate ( map ( lambda x: x.split(), str_list), 1 ) for sub in val])))
res = []
for ele in temp[ 0 ]:
res.append(test_list[ele - 1 ])
print ("Mapped list elements : " + str (res))
|
Output :
The original list is : [5, 6, 3, 7, 4]
Mapped list elements : [5, 6, 6, 3, 3, 7, 4, 4]
Time complexity: O(n * m), where n is the length of the input lists and m is the maximum length of the string in the input string list.
Auxiliary space: O(n * m), as it creates a new list with the same size as the input list, where each element is a list of length m.
Method #2 : Using chain.from_iterables() + zip() This is yet another way in which this task can be performed. In this, we perform the task of flattening using chain.from_iterables() and zip() is used to perform parallel iteration.
Python3
from itertools import chain
test_list = [ 5 , 6 , 3 , 7 , 4 ]
print ("The original list is : " + str (test_list))
str_list = [ 'Gfg' , 'is best' , 'I love' , 'Gfg' , 'and CS' ]
res = list (chain.from_iterable([[sub1] * len (sub2.split()) for sub1, sub2 in zip (test_list, str_list)]))
print ("Mapped list elements : " + str (res))
|
Output :
The original list is : [5, 6, 3, 7, 4]
Mapped list elements : [5, 6, 6, 3, 3, 7, 4, 4]
Time complexity: O(nm), where n is the length of test_list and m is the total number of words in str_list.
Auxiliary Space: O(m), as we are storing the result in a list res that can potentially contain m elements,
Method #3: Using map(), split(), and repeat():
Step-by-step approach:
- Initialize the test_list and str_list lists.
- Map the str.split() function to each element of str_list using the map() function, creating a list of lists.
- Use the enumerate() function to generate an index for each element in the mapped list.
- For each index and split element in the mapped list, repeat the corresponding integer from test_list using the repeat() function and extend the result to the res list.
- Print the resulting res list.
Python3
from itertools import repeat
test_list = [ 5 , 6 , 3 , 7 , 4 ]
print ( "The original list is : " + str (test_list))
str_list = [ 'Gfg' , 'is best' , 'I love' , 'Gfg' , 'and CS' ]
res = []
for i, s in enumerate ( map ( str .split, str_list)):
res.extend(repeat(test_list[i], len (s)))
print ( "Mapped list elements : " + str (res))
|
Output
The original list is : [5, 6, 3, 7, 4]
Mapped list elements : [5, 6, 6, 3, 3, 7, 4, 4]
Time complexity: O(nk)
- The map() function takes O(n) time to apply the str.split() function to each element in str_list.
- The enumerate() function takes O(n) time to generate an index for each element in the mapped list.
- The extend() function takes O(k) time to add k elements to the res list.
- Therefore, the overall time complexity of the algorithm is O(nk), where n is the length of str_list and k is the maximum number of split elements in any element of str_list.
Auxiliary Space: O(n+k)
- The map() function and the mapped list each take O(n) space to store the split strings and the index tuples, respectively.
- The res list takes O(k) space to store the repeated integers.
- Therefore, the overall space complexity of the algorithm is O(n+k).
Method #4: using list comprehensions and the zip() function.
Python3
test_list = [ 5 , 6 , 3 , 7 , 4 ]
str_list = [ 'Gfg' , 'is best' , 'I love' , 'Gfg' , 'and CS' ]
splitted_str_list = [s.split() for s in str_list]
res = [num for num, lst in zip (test_list, splitted_str_list) for _ in range ( len (lst))]
print ( "Mapped list elements: " + str (res))
|
Output
Mapped list elements: [5, 6, 6, 3, 3, 7, 4, 4]
Time complexity: O(nm), where n is the length of the original list and m is the length of the longest sublist in the string list.
Auxiliary space: O(nm) because a new list is created to store the result.
Share your thoughts in the comments
Please Login to comment...