Skip to content
Related Articles

Related Articles

Python – Double Split String to Matrix

Improve Article
Save Article
  • Last Updated : 16 Aug, 2022
Improve Article
Save Article

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.

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)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!