Python – Double Split String to Matrix
Given a String, perform the double split, 1st for rows, and next for individual elements so that the given string can be converted to a matrix.
Examples:
Input : test_str = 'Gfg,best*for,all*geeks,and,CS', row_splt = "*", ele_splt = ","
Output : [['Gfg', 'best'], ['for', 'all'], ['geeks', 'and', 'CS']]
Explanation : String split by rows, and elements by respective delims.
Input : test_str = 'Gfg!best*for!all*geeks!and!CS', row_splt = "*", ele_splt = "!"
Output : [['Gfg', 'best'], ['for', 'all'], ['geeks', 'and', 'CS']]
Explanation : String split by rows, and elements by respective delims.
Method #1 : Using split() + loop
In this, 1st split() is used to construct rows of Matrix, and then nested split() to get separation between individual elements.
Step by step approach :
- We start by importing the necessary Python modules and initializing the string variable test_str to the value ‘Gfg,best#for,all#geeks,and,CS’.
- We print the original string using the print() function and string concatenation.
- We initialize the variables row_splt and ele_splt to the characters ‘#’ and ‘,’ respectively.
- We use the split() function to split the string test_str into a list of substrings using the row_splt character as the delimiter. This returns a list of three substrings: ‘Gfg,best’, ‘for,all’, and ‘geeks,and,CS’. We store this list in the variable temp.
- We create an empty list called res.
- We loop through each element ele in the list temp.
- For each ele, we split it into a list of substrings using the ele_splt character as the delimiter. For example, the first iteration of the loop splits the string ‘Gfg,best’ into the list [‘Gfg’, ‘best’].
- We append the resulting list to the res list.
- After the loop has finished executing, the res list contains three lists, each of which represents a row in the matrix.
- We print the resulting matrix by converting the res list to a string using the str() function and concatenating it with a message.
Python3
test_str = 'Gfg,best#for,all#geeks,and,CS'
print ( "The original string is : " + str (test_str))
row_splt = "#"
ele_splt = ","
temp = test_str.split(row_splt)
res = []
for ele in temp:
res.append(ele.split(ele_splt))
print ( "String after Matrix conversion : " + str (res))
|
Output
The original string is : Gfg,best#for,all#geeks,and,CS
String after Matrix conversion : [['Gfg', 'best'], ['for', 'all'], ['geeks', 'and', 'CS']]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using list comprehension + split()
This is yet another way in which this task can be performed. In this, we use a similar process, but one-liner to solve the problem.
Python3
test_str = 'Gfg,best#for,all#geeks,and,CS'
print ( "The original string is : " + str (test_str))
row_splt = "#"
ele_splt = ","
temp = test_str.split(row_splt)
res = [ele.split(ele_splt) for ele in temp]
print ( "String after Matrix conversion : " + str (res))
|
Output
The original string is : Gfg,best#for,all#geeks,and,CS
String after Matrix conversion : [['Gfg', 'best'], ['for', 'all'], ['geeks', 'and', 'CS']]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4: Using regular expressions
In the above code, we first split the string into rows and elements using the re.split() method and the row_splt and ele_splt delimiters. Then, we create a matrix out of the resulting list of lists.
Python3
import re
test_str = 'Gfg,best#for,all#geeks,and,CS'
print ( "The original string is : " + str (test_str))
row_splt = "#"
ele_splt = ","
res = [re.split(ele_splt, x) for x in re.split(row_splt, test_str)]
print ( "String after Matrix conversion : " + str (res))
|
Output
The original string is : Gfg,best#for,all#geeks,and,CS
String after Matrix conversion : [['Gfg', 'best'], ['for', 'all'], ['geeks', 'and', 'CS']]
Time complexity: O(n*m), where n is the number of rows and m is the number of elements in each row.
Auxiliary space: O(n*m), as we need to create a list of lists to store the resulting matrix.
Method #4: Using map() + lambda function
Here is an alternative approach using the map() function and lambda function to split the string into rows and elements.
Steps:
- Initialize the string and the row and element split characters.
- Use the map() function with lambda function to split the string into rows and then split each row into elements.
- Convert the resulting map object into a list and print the output.
Python3
test_str = 'Gfg,best#for,all#geeks,and,CS'
print ( "The original string is : " + str (test_str))
row_splt = "#"
ele_splt = ","
res = list ( map ( lambda x: x.split(ele_splt), test_str.split(row_splt)))
print ( "String after Matrix conversion : " + str (res))
|
Output
The original string is : Gfg,best#for,all#geeks,and,CS
String after Matrix conversion : [['Gfg', 'best'], ['for', 'all'], ['geeks', 'and', 'CS']]
Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(n), where n is the length of the input string (for storing the output list).
Method #5: Using NumPy
The program imports the NumPy library using import numpy as np.
The program initializes a string called test_str with the value ‘Gfg,best#for,all#geeks,and,CS’.
The program then prints the original string using the print() function and string concatenation.
The program initializes a variable called row_splt with the value #, and a variable called ele_splt with the value ,. These variables represent the characters used to split the string into rows and elements.
The program splits the string into a list of rows using the row_splt character, and then splits each row into a list of elements using the ele_splt character, using a nested list comprehension.
The program creates a NumPy array from the resulting list using the np.array() function.
The program then prints the resulting NumPy array using the print() function.
Python3
import numpy as np
test_str = 'Gfg,best#for,all#geeks,and,CS'
print ( "The original string is : " + str (test_str))
row_splt = "#"
ele_splt = ","
rows = [row.split(ele_splt) for row in test_str.split(row_splt)]
max_len = max ( len (row) for row in rows)
rows_padded = [row + [""] * (max_len - len (row)) for row in rows]
res = np.array(rows_padded)
print ( "String after Matrix conversion : " + str (res))
|
OUTPUT:
The original string is : Gfg,best#for,all#geeks,and,CS
String after Matrix conversion : [['Gfg' 'best' '']
['for' 'all' '']
['geeks' 'and' 'CS']]
Time complexity: O(n^2) for both creating the list of rows and elements and for converting the list to a NumPy array.
Auxiliary space: O(n^2) for the list of rows and elements, and O(n^2) for the NumPy array.
Last Updated :
12 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...