Open In App

Python – Convert Matrix to Sets of Set

Last Updated : 12 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate working of
# Convert Matrix to Sets of Set
# Using set() + frozenset() + generator expression
 
# initializing list
test_list = [[5, 6, 3], [1, 7, 9], [8, 2, 0]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# frozenset() to get inner elements hashable required for set()
res = set(frozenset(sub) for sub in test_list)
 
# printing 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})}

Method #2: Using map() + frozenset() + set()

In this, map() is used to extend logic of converting inner containers to frozenset().

Python3




# Python3 code to demonstrate working of
# Convert Matrix to Sets of Set
# Using map() + frozenset() + set()
 
# initializing list
test_list = [[5, 6, 3], [1, 7, 9], [8, 2, 0]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# map() to extend logic to inner elements
res = set(map(frozenset, test_list))
 
# printing 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})}

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




# Python3 code to demonstrate working of
# Convert Matrix to Sets of Set
# Using list comprehension and frozenset
 
# initializing list
test_list = [[5, 6, 3], [1, 7, 9], [8, 2, 0]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# list comprehension to convert each sublist to frozenset
result = [frozenset(sub) for sub in test_list]
 
# convert the list of frozensets to a set
res = set(result)
 
# printing 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 :

  1. Import the reduce function from functools module.
  2. Initialize the list of lists.
  3. Use reduce function to convert list of lists to set of sets by applying union operation on each frozenset of inner lists.
  4. The set function is called with an empty set as the initial value to create an empty set.
  5. Store the result in a variable.
  6.  
  7. Print the result.

Python3




# Python3 code to demonstrate working of
# Convert Matrix to Sets of Set
# Using reduce()
 
from functools import reduce
 
# initializing list
test_list = [[5, 6, 3], [1, 7, 9], [8, 2, 0]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# using reduce() to convert list of lists to set of sets
res = reduce(lambda x, y: x.union({frozenset(y)}), test_list, set())
 
# printing result
print("Converted set Matrix : " + str(res))
 
#This code is contributed by Jyothi pinjala.


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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads