![]() |
VOOZH | about |
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.
The 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.
The 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:
Below is the implementation of the above approach:
The 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.
The 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:
Below is the implementation of the above approach:
The 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.