Skip to content
Related Articles
Open in App
Not now

Related Articles

Python – Convert Integer Matrix to String Matrix

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 13 Mar, 2023
Improve Article
Save Article

Given a matrix with integer values, convert each element to String.

Input : test_list = [[4, 5, 7], [10, 8, 3], [19, 4, 6]]
Output : [['4', '5', '7'], ['10', '8', '3'], ['19', '4', '6']] 
Explanation : All elements of Matrix converted to Strings. 
Input : test_list = [[4, 5, 7], [10, 8, 3]] 
Output : [['4', '5', '7'], ['10', '8', '3']] 
Explanation : All elements of Matrix converted to Strings.

Method #1 : Using str() + list comprehension

The combination of the above methods can be used to solve this problem. In this, we perform the conversion using str(), and list comprehension is used to iterate for all the elements.

Python3




# Python3 code to demonstrate working of
# Convert Integer Matrix to String Matrix
# Using str() + list comprehension
 
# initializing list
test_list = [[4, 5, 7], [10, 8, 3], [19, 4, 6], [9, 3, 6]]
 
# printing original list
print("The original list : " + str(test_list))
 
# using str() to convert each element to string
res = [[str(ele) for ele in sub] for sub in test_list]
 
# printing result
print("The data type converted Matrix : " + str(res))

Output

The original list : [[4, 5, 7], [10, 8, 3], [19, 4, 6], [9, 3, 6]]
The data type converted Matrix : [['4', '5', '7'], ['10', '8', '3'], ['19', '4', '6'], ['9', '3', '6']]

Method #2 : Using str() + map()

The combination of above functions can also be used to solve this problem. In this, we use map() to extend the string conversion to all row elements.

Python3




# Python3 code to demonstrate working of
# Convert Integer Matrix to String Matrix
# Using str() + map()
 
# initializing list
test_list = [[4, 5, 7], [10, 8, 3], [19, 4, 6], [9, 3, 6]]
 
# printing original list
print("The original list : " + str(test_list))
 
# using map() to extend all elements as string 
res = [list(map(str, sub)) for sub in test_list]
 
# printing result
print("The data type converted Matrix : " + str(res))

Output

The original list : [[4, 5, 7], [10, 8, 3], [19, 4, 6], [9, 3, 6]]
The data type converted Matrix : [['4', '5', '7'], ['10', '8', '3'], ['19', '4', '6'], ['9', '3', '6']]

The Time and Space Complexity for all the methods are the same:

Time Complexity: O(n2)
Auxiliary Space: O(n)

Method #3: Using numpy.char.mod() function.

Algorithm:

  1. Convert the input list into a numpy array using numpy.array() function.
  2. Use numpy.char.mod() function to convert the elements of the array to strings. %s format specifier is used to indicate string conversion.
  3. Convert the resulting numpy array back to a list using tolist() method.

Python3




import numpy as np
# initializing list
test_list = [[4, 5, 7], [10, 8, 3], [19, 4, 6], [9, 3, 6]]
 
# printing original list
print("The original list : " + str(test_list))
 
matrix = np.array(test_list)
res = np.char.mod('%s', matrix).tolist()
 
# printing result
print("The data type converted Matrix : " + str(res))

Output

The original list : [[4, 5, 7], [10, 8, 3], [19, 4, 6], [9, 3, 6]]
The data type converted Matrix : [[‘4’, ‘5’, ‘7’], [’10’, ‘8’, ‘3’], [’19’, ‘4’, ‘6’], [‘9’, ‘3’, ‘6’]]

Time Complexity:

The time complexity of this algorithm is O(m * n), where m is the number of rows and n is the number of columns in the input matrix. This is because we are iterating over each element of the matrix once to convert it to a string.

Space Complexity:

The space complexity of this algorithm is also O(m * n), as we are creating a new numpy array of the same size as the input matrix and then converting it to a list. However, the memory usage of numpy arrays is typically more efficient than Python lists, so this method may be more memory-efficient than other methods that use Python lists.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!