Python - Convert Matrix to Coordinate Dictionary
Last Updated :
14 Mar, 2023
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 this task can be performed.
Input : test_list = [['g', 'g', 'g'], ['g', 'g', 'g']] Output : {'g': {(0, 1), (1, 2), (0, 0), (1, 1), (1, 0), (0, 2)}} Input : test_list = [['a', 'b', 'c']] Output : {'a': {(0, 0)}, 'b': {(0, 1)}, 'c': {(0, 2)}}
Method #1 : Using loop + enumerate() The combination of above functionalities can be used to perform this task. In this, we use brute force to extract elements and assign indices to them with the help of enumerate().
Python3
# Python3 code to demonstrate working of
# Convert Matrix to Coordinate Dictionary
# Using loop + enumerate()
# initializing list
test_list = [['g', 'f', 'g'], ['i', 's', 'g'], ['b', 'e', 's', 't']]
# printing original list
print("The original list is : " + str(test_list))
# Convert Matrix to Coordinate Dictionary
# Using loop + enumerate()
res = dict()
for idx, sub in enumerate(test_list):
for j, ele in enumerate(sub):
if ele in res:
res[ele].add((idx, j))
else:
res[ele] = {(idx, j)}
# printing result
print("The Coordinate Dictionary : " + str(res))
Output :
The original list is : [['g', 'f', 'g'], ['i', 's', 'g'], ['b', 'e', 's', 't']] The Coordinate Dictionary : {'g': {(1, 2), (0, 0), (0, 2)}, 'f': {(0, 1)}, 't': {(2, 3)}, 'i': {(1, 0)}, 'b': {(2, 0)}, 'e': {(2, 1)}, 's': {(1, 1), (2, 2)}}
Time Complexity: O(n*m) where n is the total number of values in the column and m is the total number of values in the row in the list “test_list”.
Auxiliary Space: O(k) where k is the length of the list “test_list”.
Method #2 : Using setdefault() + loop This method operates in similar way as above, just the difference is that setdefault() reduces the task of memoizing the element value and key presence checks.
Python3
# Python3 code to demonstrate working of
# Convert Matrix to Coordinate Dictionary
# Using setdefault() + loop
# initializing list
test_list = [['g', 'f', 'g'], ['i', 's', 'g'], ['b', 'e', 's', 't']]
# printing original list
print("The original list is : " + str(test_list))
# Convert Matrix to Coordinate Dictionary
# Using setdefault() + loop
res = dict()
for idx, ele in enumerate(test_list):
for j, sub in enumerate(ele):
res.setdefault(sub, set()).add((idx, j))
# printing result
print("The Coordinate Dictionary : " + str(res))
Output :
The original list is : [['g', 'f', 'g'], ['i', 's', 'g'], ['b', 'e', 's', 't']] The Coordinate Dictionary : {'g': {(1, 2), (0, 0), (0, 2)}, 'f': {(0, 1)}, 't': {(2, 3)}, 'i': {(1, 0)}, 'b': {(2, 0)}, 'e': {(2, 1)}, 's': {(1, 1), (2, 2)}}
Time Complexity: O(n*n) where n is the number of elements in the dictionary. The setdefault() + loop is used to perform the task and it takes O(n*n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the dictionary.
Similar Reads
Python - Convert Coordinate Dictionary to Matrix 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 dis
4 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
Python - Character coordinates in Matrix Sometimes, while working with Python data, we can have a problem in which we need to extract all the coordinates in Matrix, which are characters. This kind of problem can have potential application in domains such as web development and day-day programming. Let's discuss certain ways in which this t
5 min read