Python Program to Merge a Matrix By the Elements of First Column
Last Updated :
08 May, 2023
Given a Matrix, perform merge on the basis of the element in the first column.
Input : test_list = [[4, “geeks”], [3, “Gfg”], [4, “CS”], [4, “cs”], [3, “best”]]
Output : [[4, ‘geeks’, ‘CS’, ‘cs’], [3, ‘Gfg’, ‘best’]]
Explanation : 4 is paired with geeks, CS and cs hence are merged into 1 row.
Input : test_list = [[4, “geeks”], [3, “Gfg”], [4, “CS”], [5, “cs”], [3, “best”]]
Output : [[4, ‘geeks’, ‘CS’, ‘cs’], [3, ‘Gfg’, ‘best’], [5, ‘cs’]]
Explanation : 4 is paired with geeks and CS hence are merged into 1 row.
Method 1 : Using setdefault() and list comprehension
In this, the task of grouping is done using setdefault(), which assigns key as first column element and rest of elements as values of list. List comprehension is used post that to get all the values from the dictionary constructed.
Python3
test_list = [[ 4 , "geeks" ], [ 3 , "Gfg" ], [ 4 , "CS" ],
[ 4 , "cs" ], [ 3 , "best" ]]
print ( "The original list is : " + str (test_list))
res = {}
for key, val in test_list:
res.setdefault(key, []).append(val)
res = [[key] + val for key, val in res.items()]
print ( "Merged Matrix : " + str (res))
|
Output:
The original list is : [[4, ‘geeks’], [3, ‘Gfg’], [4, ‘CS’], [4, ‘cs’], [3, ‘best’]] Merged Matrix : [[4, ‘geeks’, ‘CS’, ‘cs’], [3, ‘Gfg’, ‘best’]]
Time Complexity: O(m*n) where m and n is the number of rows and columns respectively in the list “test_list”. T
Auxiliary Space: O(k) additional space of size k is created
Method 2 : Using values() and setdefault()
Here, we extract the values using values(), rest all the operations are performed in a similar manner as explained above. This program omits from the list the first column element based on which grouping was performed.
Python3
test_list = [[ 4 , "geeks" ], [ 3 , "Gfg" ], [ 4 , "CS" ],
[ 4 , "cs" ], [ 3 , "best" ]]
print ( "The original list is : " + str (test_list))
res = {}
for key, val in test_list:
res.setdefault(key, []).append(val)
res = list (res.values())
print ( "Merged Matrix : " + str (res))
|
Output:
The original list is : [[4, ‘geeks’], [3, ‘Gfg’], [4, ‘CS’], [4, ‘cs’], [3, ‘best’]] Merged Matrix : [[‘geeks’, ‘CS’, ‘cs’], [‘Gfg’, ‘best’]]
Method 3: Using defaultdict and for loop
Use defaultdict from the collections module, which automatically initializes any new keys with the default value specified (in this case, an empty list). Then, iterate over the list of lists and append the second element of each list to the list corresponding to the first element (key) in the dictionary. Finally, combine the key and values into a new list of lists.
Approach:
- Initialize a list of lists containing key-value pairs.
- Create an empty dictionary to store the merged results.
- Iterate over the list of lists and for each key-value pair:
a. If the key already exists in the dictionary, append the value to the corresponding list.
b. If the key does not exist in the dictionary, create a new list with the value and add it to the dictionary with the key as the key.
- Combine the key and values for each dictionary item into a new list of lists.
- Print the final merged list.
Follow the below steps to implement the above idea:
Python3
from collections import defaultdict
test_list = [[ 4 , "geeks" ], [ 3 , "Gfg" ], [ 4 , "CS" ],
[ 4 , "cs" ], [ 3 , "best" ]]
print ( "The original list is : " + str (test_list))
res = defaultdict( list )
for key, val in test_list:
res[key].append(val)
res = [[key] + val for key, val in res.items()]
print ( "Merged Matrix : " + str (res))
|
Output
The original list is : [[4, 'geeks'], [3, 'Gfg'], [4, 'CS'], [4, 'cs'], [3, 'best']]
Merged Matrix : [[4, 'geeks', 'CS', 'cs'], [3, 'Gfg', 'best']]
Time complexity: O(n), where n is the length of the input list. This is because we iterate over the list of lists only once.
Auxiliary space: O(n), where n is the length of the input list. This is because we create a dictionary with at most n keys and values, and the final merged list also has at most n elements.
Method 4: Using itertools.groupby() function
Python3
from itertools import groupby
test_list = [[ 4 , "geeks" ], [ 3 , "Gfg" ], [ 4 , "CS" ],
[ 4 , "cs" ], [ 3 , "best" ]]
test_list.sort(key = lambda x: x[ 0 ])
grouped = groupby(test_list, key = lambda x: x[ 0 ])
res = {key: [val[ 1 ] for val in vals] for key, vals in grouped}
print ( "Merged Matrix : " + str (res))
|
Output
Merged Matrix : {3: ['Gfg', 'best'], 4: ['geeks', 'CS', 'cs']}
Time complexity: O(n log n), where n is the length of the input list.
Auxiliary space: O(n), where n is the length of the input list, since we need to store the sorted list and the dictionary.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...