Open In App

Python – Synchronized Split list with other

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 code to demonstrate working of
# Synchronized Split list with other
# Using map() + zip() + enumerate() + split()
 
# initializing list
test_list = [5, 6, 3, 7, 4]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing String list
str_list = ['Gfg', 'is best', 'I love', 'Gfg', 'and CS']
 
# Synchronized Split list with other
# Using map() + zip() + enumerate() + split()
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])
 
# printing result
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 code to demonstrate working of
# Synchronized Split list with other
# Using chain.from_iterables() + zip()
from itertools import chain
 
# initializing list
test_list = [5, 6, 3, 7, 4]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing String list
str_list = ['Gfg', 'is best', 'I love', 'Gfg', 'and CS']
 
# Synchronized Split list with other
# Using chain.from_iterables() + zip()
res = list(chain.from_iterable([[sub1] * len(sub2.split()) for sub1, sub2 in zip(test_list, str_list)]))
 
# printing result
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:




# Python3 code to demonstrate working of
# Synchronized Split list with other
# Using map() + repeat() + split()
 
from itertools import repeat
 
# initializing list
test_list = [5, 6, 3, 7, 4]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing String list
str_list = ['Gfg', 'is best', 'I love', 'Gfg', 'and CS']
 
# Synchronized Split list with other
# Using map() + repeat() + split()
res = []
for i, s in enumerate(map(str.split, str_list)):
    res.extend(repeat(test_list[i], len(s)))
 
# printing result
print("Mapped list elements : " + str(res))
#This code is contributed by Jyothi Pinjala.

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)

Auxiliary Space: O(n+k)

Method #4: using list comprehensions and the zip() function.




# Initializing list
test_list = [5, 6, 3, 7, 4]
 
# Initializing string list
str_list = ['Gfg', 'is best', 'I love', 'Gfg', 'and CS']
 
# Splitting string list
splitted_str_list = [s.split() for s in str_list]
 
# Synchronizing split lists with other
res = [num for num, lst in zip(test_list, splitted_str_list) for _ in range(len(lst))]
 
# Printing result
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.


Article Tags :