Given a List containing strings with a particular delimiter. The task is to remove the delimiter and convert the string to the list of tuple.
Examples:
Input : test_list = [“1-2”, “3-4-8-9”], K = “-”
Output : [(1, 2), (3, 4, 8, 9)]
Explanation : After splitting, 1-2 => (1, 2).
Input : test_list = [“1*2”, “3*4*8*9”], K = “*”
Output : [(1, 2), (3, 4, 8, 9)]
Explanation : After splitting, 1*2 => (1, 2).
Method #1 : Using list comprehension + split()
In this, first, each string is split using split() with K as an argument, then this is extended to all the Strings using list comprehension.
Python3
test_list = [ "1-2" , "3-4-8-9" , "4-10-4" ]
print ( "The original list is : " + str (test_list))
K = "-"
res = [ tuple ( int (ele) for ele in sub.split(K)) for sub in test_list]
print ( "The converted tuple list : " + str (res))
|
OutputThe original list is : ['1-2', '3-4-8-9', '4-10-4']
The converted tuple list : [(1, 2), (3, 4, 8, 9), (4, 10, 4)]
Time Complexity: O(n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list “test_list”.
Method #2 : Using map() + split() + list comprehension
In this, the task of extension of integral extension logic is done using map() and then list comprehension is used to perform the task of construction of the list.
Python3
test_list = [ "1-2" , "3-4-8-9" , "4-10-4" ]
print ( "The original list is : " + str (test_list))
K = "-"
res = [ tuple ( map ( int , sub.split(K))) for sub in test_list]
print ( "The converted tuple list : " + str (res))
|
OutputThe original list is : ['1-2', '3-4-8-9', '4-10-4']
The converted tuple list : [(1, 2), (3, 4, 8, 9), (4, 10, 4)]
The time complexity is O(n*m), where n is the number of elements in the list and m is the maximum length of a single element in the list.
The Auxiliary space is O(nm)
Method #3: Using for loop and append() method
Iterate through each string of the list using a for loop, split each string by the K delimiter, convert the split strings to integers using the map() function, and append the tuple of integers to a result list.
Step-by-step approach:
- Initialize an empty list called ‘result‘ to store the converted tuples.
- Use a for loop to iterate through each string in the input list, ‘test_list‘.
- Inside the for loop, split each string by the K delimiter using the split() method and store the split strings in a list called ‘split_list‘.
- Use the map() function with int() to convert each string in the ‘split_list‘ to an integer, and store the result in a new list called ‘int_list‘.
- Create a tuple from the ‘int_list‘ using the tuple() function, and append it to the ‘result’ list.
- After the for loop is completed, print the ‘result‘ list.
Below is the implementation of the above approach:
Python3
test_list = [ "1-2" , "3-4-8-9" , "4-10-4" ]
print ( "The original list is : " + str (test_list))
K = "-"
result = []
for sub in test_list:
split_list = sub.split(K)
int_list = list ( map ( int , split_list))
result.append( tuple (int_list))
print ( "The converted tuple list : " + str (result))
|
OutputThe original list is : ['1-2', '3-4-8-9', '4-10-4']
The converted tuple list : [(1, 2), (3, 4, 8, 9), (4, 10, 4)]
Time complexity: O(nm), where ‘n’ is the number of strings in the input list and ‘m’ is the average number of integers in each string.
Auxiliary space: O(nm), for the ‘result’ list that stores the converted tuples.
Method #4: Using regex and for loop
We can also use regular expressions to split the strings at the delimiter and convert the resulting strings to integers using a for loop and the append() method.
- Import the ‘re’ module for working with regular expressions.
- Initialize a list named ‘test_list’ with some sample data that contains strings separated by a delimiter ‘-‘.
- Print the original list to show the input data.
- Initialize a variable named ‘K’ with the delimiter used to separate the strings.
- Create a regular expression pattern using the ‘re.compile()’ method that matches any sequence of one or more digits.
- Initialize an empty list named ‘res’ to store the resulting tuples.
- Iterate through each string in the ‘test_list’ using a ‘for’ loop.
- Use the ‘pattern.findall()’ method to find all the substrings in the current string that match the pattern.
- Convert each substring to an integer using a ‘for’ loop and the ‘int()’ method.
- Combine the resulting integers into a tuple using the ‘tuple()’ method.
- Append the tuple to the ‘res’ list.
- Print the final ‘res’ list to show the converted tuple list.
Python3
import re
test_list = [ "1-2" , "3-4-8-9" , "4-10-4" ]
print ( "The original list is : " + str (test_list))
K = "-"
pattern = re. compile (r '\d+' )
res = []
for sub in test_list:
sub_list = pattern.findall(sub)
sub_tuple = tuple ( int (num) for num in sub_list)
res.append(sub_tuple)
print ( "The converted tuple list : " + str (res))
|
OutputThe original list is : ['1-2', '3-4-8-9', '4-10-4']
The converted tuple list : [(1, 2), (3, 4, 8, 9), (4, 10, 4)]
Time Complexity: The time complexity of this method is O(n*m), where n is the length of the input list and m is the average length of each string in the input list.
Auxiliary Space: The auxiliary space used by this method is O(n*m), as we are creating a new list to store the resulting tuples.
Method #6: Using a lambda function with map() and split()
In this approach, we can use a lambda function with map() and split() to split each element of the given list by the separator “-” and convert the resulting sublists of strings into tuples of integers.
Step-by-step approach:
- Initialize the given list.
- Define a lambda function that takes a string as input, splits it by “-“, and maps the resulting sublists of strings to tuples of integers using the map() function.
- Apply the lambda function to each element of the given list using the map() function.
- Convert the resulting map object to a list using the list() function and store it in a variable.
- Print the resulting list.
Below is the implementation of the above approach:
Python3
test_list = [ "1-2" , "3-4-8-9" , "4-10-4" ]
print ( "The original list is : " + str (test_list))
split_and_convert = lambda s: tuple ( map ( int , s.split( "-" )))
result = list ( map (split_and_convert, test_list))
print ( "The converted tuple list : " + str (result))
|
OutputThe original list is : ['1-2', '3-4-8-9', '4-10-4']
The converted tuple list : [(1, 2), (3, 4, 8, 9), (4, 10, 4)]
Time complexity: O(nm), where n is the length of the given list and m is the maximum number of elements in any sublist after splitting.
Auxiliary space: O(nm), where n is the length of the given list and m is the maximum number of elements in any sublist after splitting.