Python Program to Construct n*m Matrix from List
Given a list, the task is to write a python program that can construct n*m matrix.
Input : test_list = [6, 3, 7, 2, 6, 8, 4, 3, 9, 2, 1, 3], n, m = 3, 5
Output : “Matrix Not Possible”
Explanation : List has 12 elements and 3*5 is 15, hence Matrix not possible.
Input : test_list = [6, 3, 7, 2, 6, 8], n, m = 2, 3
Output : [[6, 3, 7], [2, 6, 8]]
Explanation : List converted to 2*3 matrix.
Method : Using nested loop
The constraint of n*m restricts size of list to be strictly n*m. Post achieving that, we can assign the appropriate rows and columns using nested loops.
Example 1:
Python3
test_list = [ 6 , 3 , 7 , 2 , 6 , 8 , 4 , 3 , 9 , 2 , 1 , 3 ]
print ( "The original list is : " + str (test_list))
n, m = 3 , 4
k = 0
res = []
if n * m ! = len (test_list):
res = "Matrix Not Possible"
else :
for idx in range ( 0 , n):
sub = []
for jdx in range ( 0 , m):
sub.append(test_list[k])
k + = 1
res.append(sub)
print ( "Constructed Matrix : " + str (res))
|
Output:
The original list is : [6, 3, 7, 2, 6, 8, 4, 3, 9, 2, 1, 3]
Constructed Matrix : [[6, 3, 7, 2], [6, 8, 4, 3], [9, 2, 1, 3]]
Time Complexity: O(n*m)
Auxiliary Space: O(n*m)
Example 2 :
Python3
test_list = [ 6 , 3 , 7 , 2 , 6 , 8 , 4 , 3 , 9 , 2 , 1 , 3 ]
print ( "The original list is : " + str (test_list))
n, m = 5 , 4
k = 0
res = []
if n * m ! = len (test_list):
res = "Matrix Not Possible"
else :
for idx in range ( 0 , n):
sub = []
for jdx in range ( 0 , m):
sub.append(test_list[k])
k + = 1
res.append(sub)
print ( "Constructed Matrix : " + str (res))
|
Output:
The original list is : [6, 3, 7, 2, 6, 8, 4, 3, 9, 2, 1, 3]
Constructed Matrix : Matrix Not Possible
Time Complexity: O(n*m)
Auxiliary Space: O(n*m)
Approach#3: Using zip()
In this approach, the zip() function is used to group the list elements into rows of length m, and the resulting tuples are converted to lists using a list comprehension. The * operator is used to unpack the list into individual arguments, and the iter() function is used to create an iterator over the list to be able to group it into rows of length m. Finally, the list() function is used to convert the tuples to lists.
Algorithm
1. Check if the given list input can be converted to an n x m matrix or not.
2. If possible, create the matrix using the zip() function to group the list elements into rows of length m.
3. Convert the resulting tuples to lists and print the matrix.
Python3
test_list = [ 6 , 3 , 7 , 2 , 6 , 8 ]
n, m = 2 , 3
if n * m ! = len (test_list):
print ( "Matrix Not Possible" )
else :
matrix = [ list (row) for row in zip ( * [ iter (test_list)] * m)]
print (matrix)
|
Output
[[6, 3, 7], [2, 6, 8]]
Time Complexity: O(n), where n is the number of elements in the input list. The iter() function and the zip() function both iterate over the list only once, and the list comprehension iterates over the resulting tuples once as well.
Space Complexity: O(n), where n is the number of elements in the input list. The space complexity is dominated by the resulting matrix, which has n elements.
Approach#4: Using numpy:
1. Initialize list of elements.
2. Initialize n and m.
3. Create a NumPy array from the list of elements.
4. Reshape the NumPy array to have n rows and m columns.
5. Print the resulting matrix.
Python3
import numpy as np
test_list = [ 6 , 3 , 7 , 2 , 6 , 8 , 4 , 3 , 9 , 2 , 1 , 3 ]
print ( "The original list is : " + str (test_list))
n, m = 3 , 4
arr = np.array(test_list)
res = arr.reshape(n, m)
print ( "Constructed Matrix : \n" + str (res))
|
Output:
The original list is : [6, 3, 7, 2, 6, 8, 4, 3, 9, 2, 1, 3]
Constructed Matrix :
[[6 3 7 2]
[6 8 4 3]
[9 2 1 3]]
Time Complexity:
The time complexity is O(N), where N is the total number of elements in the input list. This is because we need to create a NumPy array and then reshape it, both of which take linear time with respect to the number of elements.
Auxiliary Space:
The space complexity is O(N), where N is the total number of elements in the input list. This is because we create a NumPy array that has the same number of elements as the input list, and the reshaping operation does not increase the total amount of space used.
Last Updated :
02 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...