Python – Matrix Data Type Rectification
Sometimes, while working with data, we can have problems in which we need to perform rectification of list data types, i.e convert the Number Strings to Numbers where necessary. This can occur in Matrix form as well. Lets discuss certain ways in which this task can be performed.
Method #1 : Using list comprehension + isdigit() This is one of the way in which this task can be performed. In this, we iterate each element of Matrix and check for digit string using typecasting and then perform a conversion.
Python3
test_list = [[ '5' , 'GFG' ], [ '1' , '3' ], [ 'is' , '11' ], [ '1' , 'best' ]]
print ("The original list is : " + str (test_list))
res = [[ int (ele) if ele.isdigit() else ele for ele in sub] for sub in test_list]
print ("The rectified Matrix is : " + str (res))
|
Output :
The original list is : [['5', 'GFG'], ['1', '3'], ['is', '11'], ['1', 'best']]
The rectified Matrix is : [[5, 'GFG'], [1, 3], ['is', 11], [1, 'best']]
Time complexity: O(m*n), because it performs the same number of iterations as the original code.
Auxiliary space: O(m*n) as well, because it creates a list with m * n keys and a list of m * n elements
Method #2 : Using map() + isdigit() This is yet another way in which this task can be performed. In this, we extend the logic to each element using map() rather than list comprehension.
Python3
test_list = [[ '5' , 'GFG' ], [ '1' , '3' ], [ 'is' , '11' ], [ '1' , 'best' ]]
print ("The original list is : " + str (test_list))
res = [ list ( map ( lambda ele: int (ele) if ele.isdigit() else ele, sub)) for sub in test_list]
print ("The rectified Matrix is : " + str (res))
|
Output :
The original list is : [['5', 'GFG'], ['1', '3'], ['is', '11'], ['1', 'best']]
The rectified Matrix is : [[5, 'GFG'], [1, 3], ['is', 11], [1, 'best']]
Method #3 : Using re (regular expressions) library
This method makes use of the re library in python which allows for pattern matching in strings. Here, we use re.search() function to check if the element is a digit string or not and then convert it to int if it is.
Python3
import re
test_list = [[ '5' , 'GFG' ], [ '1' , '3' ], [ 'is' , '11' ], [ '1' , 'best' ]]
print ( "The original list is : " + str (test_list))
res = [[ int (ele) if re.search( "\d" , ele) else ele for ele in sub] for sub in test_list]
print ( "The rectified Matrix is : " + str (res))
|
Output
The original list is : [['5', 'GFG'], ['1', '3'], ['is', '11'], ['1', 'best']]
The rectified Matrix is : [[5, 'GFG'], [1, 3], ['is', 11], [1, 'best']]
Time complexity: O(n^2) where n is the number of elements in the matrix
Space complexity: O(n^2) where n is the number of elements in the matrix
Method #4 : Using for loops
Python3
test_list = [[ '5' , 'GFG' ], [ '1' , '3' ], [ 'is' , '11' ], [ '1' , 'best' ]]
print ( "The original list is : " + str (test_list))
res = []
for i in test_list:
x = []
for j in i:
if j in "0123456789" :
x.append( int (j))
else :
x.append(j)
res.append(x)
print ( "The rectified Matrix is : " + str (res))
|
Output
The original list is : [['5', 'GFG'], ['1', '3'], ['is', '11'], ['1', 'best']]
The rectified Matrix is : [[5, 'GFG'], [1, 3], ['is', '11'], [1, 'best']]
Time complexity: O(n^2) where n is the number of elements in the matrix
Auxiliary Space: O(n^2) where n is the number of elements in the matrix
Last Updated :
20 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...