Open In App
Related Articles

Python – Convert Integer Matrix to String Matrix

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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.

Iterative String Conversion

We can iterate through each element of the matrix and convert it to a string using the str() function. We will store the converted elements in a new matrix of the same size as the original matrix.

Steps:

  1. Initialize an empty matrix with the same size as the input matrix.
  2. Iterate through each element in the input matrix using nested loops.
  3. Convert each element to string using the str() function and store it in the corresponding location in the new matrix.
  4. Return the new matrix. 
     

Python3

def convert_matrix_to_string(test_list):
    rows = len(test_list)
    cols = len(test_list[0])
    string_matrix = [[''] * cols for _ in range(rows)]
    for i in range(rows):
        for j in range(cols):
            string_matrix[i][j] = str(test_list[i][j])
    return string_matrix
 
test_list = [[4, 5, 7], [10, 8, 3], [19, 4, 6]]
string_matrix = convert_matrix_to_string(test_list)
print(string_matrix)

                    

Output
[['4', '5', '7'], ['10', '8', '3'], ['19', '4', '6']]

The time complexity of this approach is O(m*n), where m and n are the dimensions of the input matrix. 
The auxiliary space used by this approach is also O(m*n).

Method 5: Using a nested loop.

Step-by-step approach:

  • Initialize an empty list to store the converted string matrix.
  • Use a nested loop to iterate over the elements of the integer matrix.
  • Convert each element to a string using the str() function and append it to a temporary list.
  • After converting all the elements in a row, append the temporary list to the result list.
  • Repeat the above steps for all the rows in the integer matrix.
  • Print the original list and the converted string matrix.

Python3

# 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))
 
# converting integer matrix to string matrix
str_matrix = []
for row in test_list:
    str_row = []
    for element in row:
        str_row.append(str(element))
    str_matrix.append(str_row)
 
# printing result
print("The data type converted Matrix : " + str(str_matrix))

                    

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: O(mn), where m is the number of rows and n is the number of columns in the matrix.
Auxiliary space: O(mn), to store the converted string matrix.



Last Updated : 23 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads