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
# Python3 code to demonstrate working of
# Convert Coordinate Dictionary to Matrix
# Using loop + max() + list comprehension
# initializing dictionary
test_dict = { (0, 1) : 4, (2, 2) : 6, (3, 1) : 7, (1, 2) : 10, (3, 2) : 11}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# Convert Coordinate Dictionary to Matrix
# Using loop + max() + list comprehension
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
# printing result
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
# Python3 code to demonstrate working of
# Convert Coordinate Dictionary to Matrix
# Using list comprehension
# initializing dictionary
test_dict = { (0, 1) : 4, (2, 2) : 6, (3, 1) : 7, (1, 2) : 10, (3, 2) : 11}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# Convert Coordinate Dictionary to Matrix
# Using list comprehension
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)]
# printing result
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)
Similar Reads
Python - Convert Matrix to Coordinate Dictionary Sometimes, while working with Python dictionaries, we can have problem in which we need to perform the conversion of matrix elements to their coordinate list. This kind of problem can come in many domains including day-day programming and competitive programming. Lets discuss certain ways in which t
3 min read
Python - Convert Matrix to Dictionary The task of converting a matrix to a dictionary in Python involves transforming a 2D list or matrix into a dictionary, where each key represents a row number and the corresponding value is the row itself. For example, given a matrix li = [[5, 6, 7], [8, 3, 2], [8, 2, 1]], the goal is to convert it i
4 min read
Python - Columns to Dictionary Conversion in Matrix Given a Matrix, Convert to Dictionary with elements in 1st row being keys, and subsequent rows acting as values list. Input : test_list = [[4, 5, 7], [10, 8, 4], [19, 4, 6], [9, 3, 6]] Output : {4: [10, 19, 9], 5: [8, 4, 3], 7: [4, 6, 6]} Explanation : All columns mapped with 1st row elements. Eg. 4
3 min read
Convert Matrix to Dictionary Value List - Python We are given a matrix and the task is to map each column of a matrix to customized keys from a list. For example, given a matrix li = [[4, 5, 6], [1, 3, 5], [3, 8, 1], [10, 3, 5]] and a list map_li = [4, 5, 6], the goal is to map the first column to the key 4, the second column to the key 5, and the
3 min read
Convert List of Dictionaries to Dictionary of Lists - Python We are given a list of dictionaries we need to convert it to dictionaries of lists. For example, we are given a list of dictionaries li = [{'manoj': 'java', 'bobby': 'python'}, {'manoj': 'php', 'bobby': 'java'}, {'manoj': 'cloud', 'bobby': 'big-data'}] we need to convert this to dictionary of list s
3 min read