Python – Convert Coordinate Dictionary to Matrix
Last Updated :
25 Apr, 2023
Sometimes, while working with Python Matrix, we can have problem in which we have dictionary records with key as matrix position and its value, and we wish to convert that to actual Matrix. This can have applications in many domains including competitive programming and day-day programming. Lets discuss certain ways in which this task can be performed.
Method #1 : Using loop + max() + list comprehension The combination of above methods can be used to solve this problem. In this, we use max() to get the dimensions of matrix, list comprehension to create matrix and loop to assign values.
Python3
test_dict = { ( 0 , 1 ) : 4 , ( 2 , 2 ) : 6 , ( 3 , 1 ) : 7 , ( 1 , 2 ) : 10 , ( 3 , 2 ) : 11 }
print ("The original dictionary is : " + str (test_dict))
temp_x = max ([cord[ 0 ] for cord in test_dict.keys()])
temp_y = max ([cord[ 1 ] for cord in test_dict.keys()])
res = [[ 0 ] * (temp_y + 1 ) for ele in range (temp_x + 1 )]
for (i, j), val in test_dict.items():
res[i][j] = val
print ("The dictionary after creation of Matrix : " + str (res))
|
Output :
The original dictionary is : {(0, 1): 4, (1, 2): 10, (3, 2): 11, (3, 1): 7, (2, 2): 6} The dictionary after creation of Matrix : [[0, 4, 0], [0, 0, 10], [0, 0, 6], [0, 7, 11]]
Time Complexity: O(n*n), where n is the length of the list test_list
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Method #2 : Using list comprehension This is yet another way in which this task can be performed. This performs task similar to above function, just the difference is that it is shorthand to above method.
Python3
test_dict = { ( 0 , 1 ) : 4 , ( 2 , 2 ) : 6 , ( 3 , 1 ) : 7 , ( 1 , 2 ) : 10 , ( 3 , 2 ) : 11 }
print ("The original dictionary is : " + str (test_dict))
temp_x, temp_y = map ( max , zip ( * test_dict))
res = [[test_dict.get((j, i), 0 ) for i in range (temp_y + 1 )]
for j in range (temp_x + 1 )]
print ("The dictionary after creation of Matrix : " + str (res))
|
Output :
The original dictionary is : {(0, 1): 4, (1, 2): 10, (3, 2): 11, (3, 1): 7, (2, 2): 6} The dictionary after creation of Matrix : [[0, 4, 0], [0, 0, 10], [0, 0, 6], [0, 7, 11]]
Using nested loops:
Approach:
Initialize an empty dictionary dictionary.
Add the key-value pairs to the dictionary. The keys are tuples representing the row and column indices, and the values are the corresponding matrix elements.
Determine the number of rows and columns needed for the matrix. This can be done by finding the maximum row and column indices from the keys of the dictionary.
Initialize a zero-filled matrix of the required size.
Loop through the key-value pairs of the dictionary, and set the corresponding elements of the matrix to the values.
Python3
dictionary = {( 0 , 1 ): 4 , ( 1 , 2 ): 10 , ( 3 , 2 ): 11 , ( 3 , 1 ): 7 , ( 2 , 2 ): 6 }
rows = max (dictionary, key = lambda x: x[ 0 ])[ 0 ] + 1
cols = max (dictionary, key = lambda x: x[ 1 ])[ 1 ] + 1
matrix = [[ 0 for _ in range (cols)] for _ in range (rows)]
for k, v in dictionary.items():
matrix[k[ 0 ]][k[ 1 ]] = v
print (matrix)
|
Output
[[0, 4, 0], [0, 0, 10], [0, 0, 6], [0, 7, 11]]
Time Complexity: O(n^2)
Auxiliary Space: O(n^2)
Share your thoughts in the comments
Please Login to comment...