Sunday, November 17, 2024
Google search engine
HomeLanguagesPython – Group similar elements into Matrix

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 the above functions provides a possible solution to this problem. In this, we perform the task of grouping using groupby() and list comprehension assists in iteration.

Python3




# 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 

Approach:

  1. Import the Counter() function from the collections module.
  2. Initialize the input list.
  3. Use the Counter() function to count the frequency of each element in the list.
  4. Use a list comprehension to create a list of sublists, where each sublist contains the key element repeated by the value count number of times.
  5. Print the resulting list of sublists.

Below is the implementation of the above approach:

Python3




# 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))
 
# Grouping similar elements into Matrix
# Using list comprehension + Counter()
temp = Counter(test_list)
res = [[key] * val for key, val in temp.items()]
 
# Printing the 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.

Method #3: Using defaultdict

defaultdict is a subclass of the built-in dict class and provides a default value for a nonexistent key. Use a defaultdict to group similar elements into a list.

Python3




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))
 
# Grouping similar elements into Matrix
# Using defaultdict
d = defaultdict(list)
 
for element in test_list:
    d[element].append(element)
res = list(d.values())
 
# Printing the 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: O(n), where n is the length of the input list, 
Auxiliary Space: 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.

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

Python3




# input list
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)):
 
        # Element found
        if i in res[j]:
            res[j].append(i)
            found = True
            break
    # Not found
    if not found:
        res.append([i])
 
        # Printing the result
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. 

Method 5: Using dictionary to group the elements of the list.

Approach:

  1. Initialize an empty dictionary group_dict.
  2. Iterate through the elements of the input list test_list.
  3. For each element, check if it is already a key in the group_dict.
  4. If it is not a key, add it as a key with a value of a list containing the element.
  5. If it is already a key, append the element to the corresponding list.
  6. Convert the dictionary to a list of lists to get the desired matrix form.
  7. Print the resulting matrix.

Below is the implementation of the above approach:

Python3




# Input list
test_list = [1, 3, 5, 1, 3, 2, 5, 4, 2]
group_dict = {}
 
# Step 2-5
for element in test_list:
    if element not in group_dict:
        group_dict[element] = [element]
    else:
        group_dict[element].append(element)
 
# Step 6
res = list(group_dict.values())
 
# Step 7
print("Matrix after grouping: " + str(res))


Output

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

Time complexity: O(n)
Auxiliary space: O(n) for the dictionary.

Method 6: Use the set() function 

Here we will get unique elements of the list, and then use list comprehension to create a new list of lists with all occurrences of each unique element.

Approach:

  1. Initialize an empty list to store the grouped elements.
  2. Use the set() function to get unique elements of the input list.
  3. Loop through each unique element.
  4. Use list comprehension to create a new list of lists with all occurrences of each unique element.
  5. Append the new list to the empty list initialized in step 1.
  6. Return the list of lists.

Below is the implementation of the above approach:

Python3




test_list = [1, 3, 5, 1, 3, 2, 5, 4, 2]
 
# Step 1
grouped = []
 
# Step 2
unique = set(test_list)
 
# Step 3
for element in unique:
     
    # Step 4
    sublist = [x for x in test_list if x == element]
     
    # Step 5
    grouped.append(sublist)
 
# Step 6
print("Matrix after grouping: " + str(grouped))


Output

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

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

Method 6: Use the set() without using list comprehension

Python3




test_list = [1, 3, 5, 1, 3, 2, 5, 4, 2]
 
# Step 1: Use set() to get unique elements of the list
unique = set(test_list)
 
# Step 2: Create a list of lists for the grouped elements
grouped = [[] for _ in range(len(unique))]
 
# Step 3: Iterate through the test_list and append each element
# to the corresponding sublist in grouped
for i, element in enumerate(unique):
    for x in test_list:
        if x == element:
            grouped[i].append(x)
 
# Step 4: Print the matrix
print("Matrix after grouping: " + str(grouped))


Output

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

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

Method 7: Using NumPy

  • Import the NumPy library
  • Use np.unique() to get unique elements of the list
  • Use np.zeros() to create a matrix of zeros with the shape (len(unique), max(counts))
  • Use nested loops to iterate through the unique elements and their counts, and fill in the matrix with the Step 5: Print the matrix

Python3




import numpy as np
 
test_list = [1, 3, 5, 1, 3, 2, 5, 4, 2]
 
# Step 1: Use np.unique() to get unique elements of the list
unique, counts = np.unique(test_list, return_counts=True)
 
# Step 2: Use np.zeros() to create a matrix of zeros with the shape (len(unique), max(counts))
matrix = np.zeros((len(unique), np.max(counts)), dtype=int)
 
# Step 3: Use nested loops to iterate through the unique elements and their counts, and fill in the matrix with the corresponding elements
for i, element in enumerate(unique):
    for j in range(counts[i]):
        matrix[i,j] = element
 
# Step 4: Remove the extra zeros
result = []
for row in matrix:
    result.append([x for x in row if x != 0])
 
# Step 5: Print the matrix
print("Matrix after grouping: " + str(result))


Output:

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

Time complexity: O(n log n) (worst case for np.unique() to sort the array)
Auxiliary space: O(n * k) (for the matrix, where k is the maximum count of an element)

RELATED ARTICLES

Most Popular

Recent Comments