Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python Program to Convert String Matrix Representation to Matrix

  • Difficulty Level : Hard
  • Last Updated : 05 Apr, 2021

Given a String with matrix representation, the task here is to write a python program that converts it to a matrix.

Input : test_str = “[gfg,is],[best,for],[all,geeks]”

Output : [[‘gfg’, ‘is’], [‘best’, ‘for’], [‘all’, ‘geeks’]]

Explanation : Required String Matrix is converted to Matrix with list as data type.

Input : test_str = “[gfg,is],[for],[all,geeks]”

Output : [[‘gfg’, ‘is’], [‘for’], [‘all’, ‘geeks’]]

Explanation : Required String Matrix is converted to Matrix with list as data type.

Method 1 : Using split() and regex expression

In this, a plain list is constructed using appropriate regex expression and split() performs the task of getting inner dimension for 2D Matrix.

Example:

Python3




import re
  
# initializing string
test_str = "[gfg,is],[best,for],[all,geeks]"
  
# printing original string
print("The original string is : " + str(test_str))
  
flat_1 = re.findall(r"\[(.+?)\]", test_str)
res = [sub.split(",") for sub in flat_1]
  
# printing result
print("The type of result : " + str(type(res)))
print("Converted Matrix : " + str(res))

Output:

The original string is : [gfg,is],[best,for],[all,geeks]

The type of result : <class ‘list’>

Converted Matrix : [[‘gfg’, ‘is’], [‘best’, ‘for’], [‘all’, ‘geeks’]]

Method 2 : Using json.loads()

In this, the task of conversion to the matrix is done using the unbuilt method of loads() of JSON library.

Example:

Python3




# Python3 code to demonstrate working of
# Convert String Matrix Representation to Matrix
# Using json.loads()
import json
  
# initializing string
test_str = '[["gfg", "is"], ["best", "for"], ["all", "geeks"]]'
  
# printing original string
print("The original string is : " + str(test_str))
  
# inbuild function performing task of conversion
# notice input
res = json.loads(test_str)
  
# printing result
print("The type of result : " + str(type(res)))
print("Converted Matrix : " + str(res))

Output:

The original string is : [[“gfg”, “is”], [“best”, “for”], [“all”, “geeks”]]

The type of result : <class ‘list’>

Converted Matrix : [[‘gfg’, ‘is’], [‘best’, ‘for’], [‘all’, ‘geeks’]]


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!