Given a Matrix, for groups according to list elements, i.e each group should contain all elements from List.
Input : test_list = [[2, 6], [7, 8], [1, 4]], check_list = [1, 2, 4, 6]
Output : [[7, 8], [[1, 2], [4, 6]]]
Explanation : 1, 2, 4, 6 elements rows are grouped.
Input : test_list = [[2, 7], [7, 8], [1, 4]], check_list = [1, 2, 4, 6]
Output : [[2, 7], [7, 8], [1, 4]]
Explanation : No grouping possible.
Method : Using loop + list comprehension + Try-Except
In this, for each row in matrix, get elements missing from list, after getting elements, match with each row if we can find missing elements if found, the new group is made.
Python3
test_list = [[ 4 , 6 ], [ 1 , 2 ], [ 2 , 6 ], [ 7 , 8 ], [ 1 , 4 ]]
print ( "The original list is : " + str (test_list))
check_list = [ 1 , 2 , 4 , 6 ]
res = []
while test_list:
sub1 = test_list.pop()
sub2 = [ele for ele in check_list if ele not in sub1]
try :
test_list.remove(sub2)
res.append([sub1, sub2])
except ValueError:
res.append(sub1)
print ( "The Grouped rows : " + str (res))
|
Output
The original list is : [[4, 6], [1, 2], [2, 6], [7, 8], [1, 4]]
The Grouped rows : [[[1, 4], [2, 6]], [7, 8], [[1, 2], [4, 6]]]
Time complexity: O(n^2), where n is the length of the input list test_list
Auxiliary space: O(n), where n is the length of the input list test_list.
Method 2: Looping over nested Loops in Python
- Each row in the test_list
- Here for each row, it creates a sublist called match to store the elements in the row that are in the check_list.
- It then loops over each element in the row, and if it finds an element that is not in the check_list, it breaks out of the loop and moves on to the next row.
Python3
test_list = [[ 4 , 6 ], [ 1 , 2 ], [ 2 , 6 ], [ 7 , 8 ], [ 1 , 4 ]]
check_list = [ 1 , 2 , 4 , 6 ]
res = []
for row in test_list:
match = []
for elem in row:
if elem in check_list:
match.append(elem)
else :
break
else :
res.append(row)
continue
non_match = []
for elem in row:
if elem not in match:
non_match.append(elem)
res.append([match, non_match])
print ( "The Grouped rows : " + str (res))
|
Output
The Grouped rows : [[4, 6], [1, 2], [2, 6], [[], [7, 8]], [1, 4]]
Time complexity: O(n*m), where n is the number of rows in the test_list and m is the maximum number of elements in any row.
Auxiliary space: O(n*m), because we create a new list to store the result, and for each row that has elements that are not in the check_list, we create two new sublists to store the matching and non-matching elements.
Method 3: Using dictionary
You can use a dictionary to group the elements in the given list based on their value. Here’s how you can do it:
Python3
test_list = [[ 4 , 6 ], [ 1 , 2 ], [ 2 , 6 ], [ 7 , 8 ], [ 1 , 4 ]]
check_list = [ 1 , 2 , 4 , 6 ]
grouped_dict = {key: [] for key in check_list}
for sublist in test_list:
for element in sublist:
if element in check_list:
grouped_dict[element].append(sublist)
grouped_list = [[grouped_dict[key], [key]] for key in check_list]
print ( "The Grouped rows : " + str (grouped_list))
|
Output
The Grouped rows : [[[[1, 2], [1, 4]], [1]], [[[1, 2], [2, 6]], [2]], [[[4, 6], [1, 4]], [4]], [[[4, 6], [2, 6]], [6]]]
Time complexity: O(nm), where n is the length of the test_list and m is the length of the check_list.
Auxiliary space: O(m + k), where m is the length of the check_list and k is the number of unique elements in test_list that are in check_list.
Method 4: Using set intersection
We can use set intersection to filter the test_list by the values in check_list and group them accordingly.
Approach:
- Define test_list as a list of lists, where each sublist contains two integers.
- Define check_list as a list of integers.
- Create an empty list grouped_list to store the result.
- Iterate through each value val in check_list.
- Create an empty list sublists to store the sublists in test_list that contain the current value val.
- Iterate through each sublist sublist in test_list.
- Check if the current value val is in the sublist sublist.
- If val is in sublist, append sublist to sublists.
- Append sublists and val as a list to grouped_list.
- Print the resulting list of lists.
Python3
test_list = [[ 4 , 6 ], [ 1 , 2 ], [ 2 , 6 ], [ 7 , 8 ], [ 1 , 4 ]]
check_list = [ 1 , 2 , 4 , 6 ]
grouped_list = []
for val in check_list:
sublists = []
for sublist in test_list:
if val in sublist:
sublists.append(sublist)
grouped_list.append([sublists, [val]])
print ( "The Grouped rows : " + str (grouped_list))
|
Output
The Grouped rows : [[[[1, 2], [1, 4]], [1]], [[[1, 2], [2, 6]], [2]], [[[4, 6], [1, 4]], [4]], [[[4, 6], [2, 6]], [6]]]
Time complexity: O(n*m), where n is the length of check_list and m is the length of the longest sublist in test_list.
Auxiliary space: O(nm), where n is the length of check_list and m is the length of the longest sublist in test_list.
Method 5: Using a nested loop and a flag variable to keep track of grouped elements
This method iterates through each row in the list and then iterates through each element in the row. If an element is in the check_list, it is added to a grouped_row list. If an element is not in the check_list, it is added to an ungrouped_row list. At the end of the iteration through each element, if there are any ungrouped elements, they are added to the grouped_list. If there are any grouped elements, they are also added to the grouped_list. This method uses a flag variable to keep track of whether any grouped elements or ungrouped elements were found in the current row.
Python3
test_list = [[ 4 , 6 ], [ 1 , 2 ], [ 2 , 6 ], [ 7 , 8 ], [ 1 , 4 ]]
check_list = [ 1 , 2 , 4 , 6 ]
grouped_list = []
for row in test_list:
grouped_row = []
ungrouped_row = []
for elem in row:
if elem in check_list:
grouped_row.append(elem)
else :
ungrouped_row.append(elem)
if ungrouped_row:
grouped_list.append(ungrouped_row)
if grouped_row:
grouped_list.append(grouped_row)
print ( "The Grouped rows : " + str (grouped_list))
|
Output
The Grouped rows : [[4, 6], [1, 2], [2, 6], [7, 8], [1, 4]]
Time complexity: O(n^2), where n is the total number of elements in the test_list.
Auxiliary space: O(n^2), since we are creating new lists for each grouped and ungrouped row, and appending these lists to the grouped_list.
Method 6: Using the built-in function filter() along with lambda function.
Step-by-step approach:
- Define the test_list and check_list:
- Use the filter() function with a lambda function to select the elements in each row that are in the check_list:
- Combine the grouped and ungrouped elements for each row into a single list using a list comprehension:
- Print the grouped_list
Below is the implementation of the above approach:
Python3
test_list = [[ 4 , 6 ], [ 1 , 2 ], [ 2 , 6 ], [ 7 , 8 ], [ 1 , 4 ]]
check_list = [ 1 , 2 , 4 , 6 ]
grouped_rows = list ( map ( lambda row: list ( filter ( lambda x: x in check_list, row)), test_list))
ungrouped_rows = list ( map ( lambda row: list ( filter ( lambda x: x not in check_list, row)), test_list))
grouped_list = [row for row in grouped_rows + ungrouped_rows if row]
print ( "The Grouped rows : " + str (grouped_list))
|
Output
The Grouped rows : [[4, 6], [1, 2], [2, 6], [1, 4], [7, 8]]
Time complexity: O(n^2), where n is the number of rows in the test_list.
Auxiliary space: O(n^2), because two separate lists (grouped_rows and ungrouped_rows) are created to hold the filtered elements for each row in the test_list.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...