Given Matrix, the task is to write a Python program to map each column’s values as customized keys from another list.
Input : test_list = [[4, 5, 6], [1, 3, 5], [3, 8, 1], [10, 3, 5]], map_list = [4, 5, 6]
Output : {4: [4, 1, 3, 10], 5: [5, 3, 8, 3], 6: [6, 5, 1, 5]}
Explanation : 4 is mapped with all the 0th index of lists, 4, 1 ,3, 10.Input : test_list = [[4, 5, 6], [1, 3, 5], [3, 8, 1]], map_list = [4, 5, 6]
Output : {4: [4, 1, 3], 5: [5, 3, 8], 6: [6, 5, 1]}
Explanation : 4 is mapped with all the 0th index of lists, 4, 1 ,3.
Method 1 : Using dictionary comprehension + zip()
In this, mapping of columns with custom list index elements is done using zip(), dictionary comprehension is done to assign extracted keys to mapping values.
Python3
# Python3 code to demonstrate working of # Convert Matrix to Dictionary Value List # Using dictionary comprehension + zip() from collections import defaultdict # initializing list test_list = [[ 4 , 5 , 6 ], [ 1 , 3 , 5 ], [ 3 , 8 , 1 ], [ 10 , 3 , 5 ]] # printing original list print ( "The original list is : " + str (test_list)) # initializing map list map_list = [ 4 , 5 , 6 ] # mapping column using zip(), dictionary comprehension for key # converts to list of dictionary temp = [{key : val for key, val in zip (map_list, idx)} for idx in test_list] # convert to dictionary value list res = defaultdict( list ) {res[key].append(sub[key]) for sub in temp for key in sub} # printing result print ( "Converted Dictionary : " + str ( dict (res))) |
The original list is : [[4, 5, 6], [1, 3, 5], [3, 8, 1], [10, 3, 5]] Converted Dictionary : {4: [4, 1, 3, 10], 5: [5, 3, 8, 3], 6: [6, 5, 1, 5]}
Time Complexity: O(n*m)
Auxiliary Space: O(k)
Method 2 : Using dict() + list comprehension + zip()
In this, the task of mapping values to dictionary keys and conversion is done using dict() and zip() and dictionary comprehension. Rest functionalities are similar to the above method.
Python3
# Python3 code to demonstrate working of # Convert Matrix to Dictionary Value List # Using dict() + list comprehension + zip() from collections import defaultdict # initializing list test_list = [[ 4 , 5 , 6 ], [ 1 , 3 , 5 ], [ 3 , 8 , 1 ], [ 10 , 3 , 5 ]] # printing original list print ( "The original list is : " + str (test_list)) # initializing map list map_list = [ 4 , 5 , 6 ] # mapping column using zip() and conversion using dict() # converts to list of dictionary temp = [ dict ( zip (map_list, sub)) for sub in test_list] # convert to dictionary value list res = defaultdict( list ) {res[key].append(sub[key]) for sub in temp for key in sub} # printing result print ( "Converted Dictionary : " + str ( dict (res))) |
The original list is : [[4, 5, 6], [1, 3, 5], [3, 8, 1], [10, 3, 5]] Converted Dictionary : {4: [4, 1, 3, 10], 5: [5, 3, 8, 3], 6: [6, 5, 1, 5]}
Method 3: Using loop
First initializes an empty dictionary res. Then, it loops over each row of the matrix test_list. For each row, it loops over each element of the row using a range function. For each element, it checks whether the corresponding key already exists in the res dictionary. If not, it creates a new key with an empty list as its value. Finally, it appends the element to the corresponding list. After the loops are completed, the resulting dictionary is printed.
Python3
# Python3 code to demonstrate working of # Convert Matrix to Dictionary Value List # Using loops # initializing list test_list = [[ 4 , 5 , 6 ], [ 1 , 3 , 5 ], [ 3 , 8 , 1 ], [ 10 , 3 , 5 ]] # printing original list print ( "The original list is : " + str (test_list)) # initializing map list map_list = [ 4 , 5 , 6 ] # convert to dictionary value list res = {} # iterate over each row of the matrix for row in test_list: # iterate over each element of the row for i in range ( len (row)): # if the key doesn't exist in the result dictionary, create an empty list if map_list[i] not in res: res[map_list[i]] = [] # append the value to the corresponding list res[map_list[i]].append(row[i]) # printing result print ( "Converted Dictionary : " + str (res)) |
The original list is : [[4, 5, 6], [1, 3, 5], [3, 8, 1], [10, 3, 5]] Converted Dictionary : {4: [4, 1, 3, 10], 5: [5, 3, 8, 3], 6: [6, 5, 1, 5]}
Time complexity: O(m*n), because it performs the same number of iterations as the original code.
Auxiliary space: O(m*n) as well, because it creates a dictionary with m * n keys and a list of m * n elements