Open In App

Python – Double Split String to Matrix

Improve
Improve
Like Article
Like
Save
Share
Report

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 :

  1. 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’.
  2. We print the original string using the print() function and string concatenation.
  3. We initialize the variables row_splt and ele_splt to the characters ‘#’ and ‘,’ respectively.
  4. 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.
  5. We create an empty list called res.
  6. We loop through each element ele in the list temp.
  7. 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’].
  8. We append the resulting list to the res list.
  9. After the loop has finished executing, the res list contains three lists, each of which represents a row in the matrix.
  10. We print the resulting matrix by converting the res list to a string using the str() function and concatenating it with a message.

Python3




# Python3 code to demonstrate working of
# Double Split String to Matrix
# Using split() + loop
 
# initializing string
test_str = 'Gfg,best#for,all#geeks,and,CS'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing row split char
row_splt = "#"
 
# initializing element split char
ele_splt = ","
 
# split for rows
temp = test_str.split(row_splt)
res = []
 
for ele in temp:
 
    # split for elements
    res.append(ele.split(ele_splt))
 
# printing result
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




# Python3 code to demonstrate working of
# Double Split String to Matrix
# Using list comprehension + split()
 
# initializing string
test_str = 'Gfg,best#for,all#geeks,and,CS'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing row split char
row_splt = "#"
 
# initializing element split char
ele_splt = ","
 
# split for rows
temp = test_str.split(row_splt)
 
# using list comprehension as shorthand
res = [ele.split(ele_splt) for ele in temp]
 
# printing result
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




# Python3 code to demonstrate working of
# Double Split String to Matrix
# Using regular expressions
 
import re
 
# initializing string
test_str = 'Gfg,best#for,all#geeks,and,CS'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing row split char
row_splt = "#"
 
# initializing element split char
ele_splt = ","
 
# split for rows and elements using regular expressions
res = [re.split(ele_splt, x) for x in re.split(row_splt, test_str)]
 
# printing result
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:

  1. Initialize the string and the row and element split characters.
  2. Use the map() function with lambda function to split the string into rows and then split each row into elements.
  3. Convert the resulting map object into a list and print the output.

Python3




# Python3 code to demonstrate working of
# Double Split String to Matrix
# Using map() + lambda function
 
# initializing string
test_str = 'Gfg,best#for,all#geeks,and,CS'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing row split char
row_splt = "#"
 
# initializing element split char
ele_splt = ","
 
# splitting string into matrix using map() and lambda function
res = list(map(lambda x: x.split(ele_splt), test_str.split(row_splt)))
 
# printing result
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
 
# initializing string
test_str = 'Gfg,best#for,all#geeks,and,CS'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing row split char
row_splt = "#"
 
# initializing element split char
ele_splt = ","
 
# splitting string into matrix using NumPy
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)
 
# printing result
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
Previous
Next
Share your thoughts in the comments
Similar Reads