Python – Group similar elements into Matrix

Sometimes, while working with Python Matrix, we can have a problem in which we need to perform grouping of all the elements with are the same. This kind of problem can have applications in data domains. Let’s discuss certain ways in which this task can be performed.

Input : test_list = [1, 3, 4, 4, 2, 3] 
Output : [[1], [2], [3, 3], [4, 4]] 

Input : test_list = [1, 3, 4, 2] 
Output : [[1], [2], [3], [4]]

Method #1: Using list comprehension + groupby() The combination of above functions provide possible solution to this problem. In this, we perform task of grouping using groupby() and list comprehension assists in iteration. 




# Python3 code to demonstrate working of
# Group similar elements into Matrix
# Using list comprehension + groupby()
from itertools import groupby
 
# initializing list
test_list = [1, 3, 5, 1, 3, 2, 5, 4, 2]
 
# printing original list
print("The original list : " + str(test_list))
 
# Group similar elements into Matrix
# Using list comprehension + groupby()
res = [list(val) for key, val in groupby(sorted(test_list))] 
         
# printing result
print("Matrix after grouping : " + str(res))

Output : 
The original list : [1, 3, 5, 1, 3, 2, 5, 4, 2]
Matrix after grouping : [[1, 1], [2, 2], [3, 3], [4], [5, 5]]

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. This is because the result list ‘res’ will have at most n elements, where each element is a list containing the grouped similar elements.

Method #2: Using list comprehension + Counter() This is yet another approach to this problem. In this, we get the values along with its frequency using Counter() and then employ list comprehension to multiply each element with frequency to get duplicates 






# Python3 code to demonstrate working of
# Group similar elements into Matrix
# Using list comprehension + Counter()
from collections import Counter
 
# initializing list
test_list = [1, 3, 5, 1, 3, 2, 5, 4, 2]
 
# printing original list
print("The original list : " + str(test_list))
 
# Group similar elements into Matrix
# Using list comprehension + Counter()
temp = Counter(test_list)
res = [[key] * val for key, val in temp.items()]
 
# printing result
print("Matrix after grouping : " + str(res))

Output : 
The original list : [1, 3, 5, 1, 3, 2, 5, 4, 2]
Matrix after grouping : [[1, 1], [2, 2], [3, 3], [4], [5, 5]]

Time complexity: The time complexity of this code is O(n log n), where n is the length of the input list. 

Auxiliary space: The auxiliary space of this code is O(n), where n is the length of the input list.

Method #3: Using defaultdict

You can also solve this problem using defaultdict. defaultdict is a subclass of the built-in dict class and provides a default value for a nonexistent key. You can use a defaultdict to group the similar elements into a list.




from collections import defaultdict
 
# initializing list
test_list = [1, 3, 5, 1, 3, 2, 5, 4, 2]
 
# printing original list
print("The original list : " + str(test_list))
 
# Group similar elements into Matrix
# Using defaultdict
d = defaultdict(list)
for element in test_list:
    d[element].append(element)
res = list(d.values())
 
# printing result
print("Matrix after grouping : " + str(res))

Output

The original list : [1, 3, 5, 1, 3, 2, 5, 4, 2]
Matrix after grouping : [[1, 1], [3, 3], [5, 5], [2, 2], [4]]

Time Complexity: The time complexity of this approach is O(n), where n is the length of the input list, 

Auxiliary Space: The space complexity of this approach is also O(n), as we are using a defaultdict to store the groups of similar elements.

Method #4: Using nested loops to iterate through the list and build the matrix.

This code first initializes an empty matrix res. It then iterates through the original list test_list and for each element i, it checks if there is already a sublist in res that contains i. If there is, it appends i to that sublist. If not, it creates a new sublist containing only i and appends it to res. At the end, res contains the desired matrix after grouping.




test_list = [1, 3, 5, 1, 3, 2, 5, 4, 2]
res = []
 
for i in test_list:
    found = False
    for j in range(len(res)):
        if i in res[j]:
            res[j].append(i)
            found = True
            break
    if not found:
        res.append([i])
 
print("Matrix after grouping : " + str(res))

Output
Matrix after grouping : [[1, 1], [3, 3], [5, 5], [2, 2], [4]]

Time complexity: O(n^2), where n is the length of test_list.
Auxiliary space: O(n), where n is the length of test_list. 


Article Tags :