Python – Convert Matrix to Sets of Set
Last Updated :
12 Apr, 2023
In this article, the task is to write Python program to Convert Matrix to sets of set.
Examples:
Input : test_list = [[5, 6, 3], [1, 7, 9], [8, 2, 0]]
Output : {frozenset({8, 0, 2}), frozenset({1, 9, 7}), frozenset({3, 5, 6})}
Explanation : Converted to sets of frozensets to remain immutable and hashable.
Input : test_list = [[5, 6, 3], [1, 7, 9]]
Output : {frozenset({1, 9, 7}), frozenset({3, 5, 6})}
Explanation : Converted to sets of frozensets to remain immutable and hashable.
Method #1: Using set() + frozenset() + generator expression
In this, we perform iteration till Matrix in generator expression to get inner sets, and outer container is converted to set using set(). Inner container needs to be hashable, hence has to be frozenset() for creation.
Python3
test_list = [[ 5 , 6 , 3 ], [ 1 , 7 , 9 ], [ 8 , 2 , 0 ]]
print ( "The original list is : " + str (test_list))
res = set ( frozenset (sub) for sub in test_list)
print ( "Converted set Matrix : " + str (res))
|
Output
The original list is : [[5, 6, 3], [1, 7, 9], [8, 2, 0]]
Converted set Matrix : {frozenset({8, 0, 2}), frozenset({1, 9, 7}), frozenset({3, 5, 6})}
Method #2: Using map() + frozenset() + set()
In this, map() is used to extend logic of converting inner containers to frozenset().
Python3
test_list = [[ 5 , 6 , 3 ], [ 1 , 7 , 9 ], [ 8 , 2 , 0 ]]
print ( "The original list is : " + str (test_list))
res = set ( map ( frozenset , test_list))
print ( "Converted set Matrix : " + str (res))
|
Output
The original list is : [[5, 6, 3], [1, 7, 9], [8, 2, 0]]
Converted set Matrix : {frozenset({8, 0, 2}), frozenset({1, 9, 7}), frozenset({3, 5, 6})}
Method #3: Using list comprehension and set
Approach
we will use a nested list comprehension to convert each row to a set, then use set and frozenset to convert the resulting list of sets to a set of frozensets.
Algorithm
1. Use a nested list comprehension to convert each row of the matrix to a set.
2. Use set and frozenset to convert the resulting list of sets to a set of frozensets.
3. Return the set of frozensets.
Python3
def matrix_to_sets(matrix):
set_list = [ set (row) for row in matrix]
return { frozenset (s) for s in set_list}
matrix = [[ 5 , 6 , 3 ], [ 1 , 7 , 9 ], [ 8 , 2 , 0 ]]
print (matrix_to_sets(matrix))
|
Output
{frozenset({8, 0, 2}), frozenset({1, 9, 7}), frozenset({3, 5, 6})}
Time complexity: O(nm), where ‘n’ is the number of rows and ‘m’ is the number of columns in the matrix.
Auxiliary Space: O(nm), since we store all the row sets and frozensets in memory.
Method #4: Using list comprehension and frozenset
Step-by-step approach:
- Initialize an empty list result.
- Loop through each sublist in test_list.
- Convert each sublist to a frozenset using frozenset(sub).
- Append the frozenset to the result list.
- Convert the result list to a set using set(result).
Below is the implementation of the above approach:
Python3
test_list = [[ 5 , 6 , 3 ], [ 1 , 7 , 9 ], [ 8 , 2 , 0 ]]
print ( "The original list is : " + str (test_list))
result = [ frozenset (sub) for sub in test_list]
res = set (result)
print ( "Converted set Matrix : " + str (res))
|
Output
The original list is : [[5, 6, 3], [1, 7, 9], [8, 2, 0]]
Converted set Matrix : {frozenset({8, 0, 2}), frozenset({1, 9, 7}), frozenset({3, 5, 6})}
Time complexity: O(n^2), where n is the number of elements in the matrix, as we need to loop through each element in each sublist.
Auxiliary space: O(n), as we create a list of frozensets with the same number of elements as the original matrix.
Method #5:Using reduce:
Algorithm :
- Import the reduce function from functools module.
- Initialize the list of lists.
- Use reduce function to convert list of lists to set of sets by applying union operation on each frozenset of inner lists.
- The set function is called with an empty set as the initial value to create an empty set.
- Store the result in a variable.
-
- Print the result.
Python3
from functools import reduce
test_list = [[ 5 , 6 , 3 ], [ 1 , 7 , 9 ], [ 8 , 2 , 0 ]]
print ( "The original list is : " + str (test_list))
res = reduce ( lambda x, y: x.union({ frozenset (y)}), test_list, set ())
print ( "Converted set Matrix : " + str (res))
|
Output
The original list is : [[5, 6, 3], [1, 7, 9], [8, 2, 0]]
Converted set Matrix : {frozenset({8, 0, 2}), frozenset({1, 9, 7}), frozenset({3, 5, 6})}
Time complexity: O(nm log m), where n is the number of inner lists and m is the maximum length of the inner lists. The frozenset() function takes O(m log m) time, and reduce() takes O(n) time. Overall, the time complexity of this algorithm is dominated by the time complexity of frozenset() function.
Space complexity: O(nm), where n is the number of inner lists and m is the maximum length of the inner lists. The space complexity is dominated by the space required to store the set of frozensets.
Share your thoughts in the comments
Please Login to comment...