Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python Program to Convert Tuple Matrix to Tuple List
Next article icon

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 listtest_list=[[4,5,7],[10,8,3],[19,4,6],[9,3,6]]# printing original listprint("The original list : "+str(test_list))# using str() to convert each element to stringres=[[str(ele)foreleinsub]forsubintest_list]# printing resultprint("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 listtest_list=[[4,5,7],[10,8,3],[19,4,6],[9,3,6]]# printing original listprint("The original list : "+str(test_list))# using map() to extend all elements as stringres=[list(map(str,sub))forsubintest_list]# printing resultprint("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
importnumpyasnp# initializing listtest_list=[[4,5,7],[10,8,3],[19,4,6],[9,3,6]]# printing original listprint("The original list : "+str(test_list))matrix=np.array(test_list)res=np.char.mod('%s',matrix).tolist()# printing resultprint("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 thestr() function and store it in the corresponding location in the new matrix.
  4. Return the new matrix. 
     
Python3
defconvert_matrix_to_string(test_list):rows=len(test_list)cols=len(test_list[0])string_matrix=[['']*colsfor_inrange(rows)]foriinrange(rows):forjinrange(cols):string_matrix[i][j]=str(test_list[i][j])returnstring_matrixtest_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 listtest_list=[[4,5,7],[10,8,3],[19,4,6],[9,3,6]]# printing original listprint("The original list : "+str(test_list))# converting integer matrix to string matrixstr_matrix=[]forrowintest_list:str_row=[]forelementinrow:str_row.append(str(element))str_matrix.append(str_row)# printing resultprint("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.


Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp