Python program to extract rows from Matrix that has distinct data types
Last Updated :
15 May, 2023
Given a Matrix, the task is to write a Python program to extract rows with no repeated data types.
Examples:
Input : test_list = [[4, 3, 1], [“gfg”, 3, {4:2}], [3, 1, “jkl”], [9, (2, 3)]]
Output : [[‘gfg’, 3, {4: 2}], [9, (2, 3)]]
Explanation : [4, 3, 1] are all integers hence omitted. [9, (2, 3)] has integer and tuple, different data types, hence included in results.
Input : test_list = [[4, 3, 1], [“gfg”, 3, {4:2}, 4], [3, 1, “jkl”], [9, (2, 3)]]
Output : [[9, (2, 3)]]
Explanation : [4, 3, 1] are all integers hence omitted. [9, (2, 3)] has integer and tuple, different data types, hence included in results.
Method 1 : Using type() + list comprehension
In this, we use type() to check for data types of each element of rows, and if the data type repeats, the row is not included in the result.
Python3
test_list = [[ 4 , 3 , 1 ], [ "gfg" , 3 , { 4 : 2 }], [ 3 , 1 , "jkl" ], [ 9 , ( 2 , 3 )]]
print ( "The original list is : " + str (test_list))
res = []
for sub in test_list:
type_size = len ( list ( set ([ type (ele) for ele in sub])))
if len (sub) = = type_size:
res.append(sub)
print ( "The Distinct data type rows : " + str (res))
|
Output
The original list is : [[4, 3, 1], ['gfg', 3, {4: 2}], [3, 1, 'jkl'], [9, (2, 3)]]
The Distinct data type rows : [['gfg', 3, {4: 2}], [9, (2, 3)]]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 2: Using type()
Python3
test_list = [[ 4 , 3 , 1 ], [ "gfg" , 3 , { 4 : 2 }], [ 3 , 1 , "jkl" ], [ 9 , ( 2 , 3 )]]
print ( "The original list is : " + str (test_list))
res = []
for sub in test_list:
c = 0
for j in range ( 0 , len (sub)):
if ( type (sub[ 0 ]) = = type (sub[j])):
c + = 1
if (c ! = len (sub)):
res.append(sub)
print ( "The Distinct data type rows : " + str (res))
|
Output
The original list is : [[4, 3, 1], ['gfg', 3, {4: 2}], [3, 1, 'jkl'], [9, (2, 3)]]
The Distinct data type rows : [['gfg', 3, {4: 2}], [3, 1, 'jkl'], [9, (2, 3)]]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 4: Using reduce()
Python3
test_list = [[ 4 , 3 , 1 ], [ "gfg" , 3 , { 4 : 2 }],
[ 3 , 1 , "jkl" ], [ 9 , ( 2 , 3 )]]
print ( "The original list is : " + str (test_list))
res = list ( filter ( lambda sub: len ( set ( map ( type , sub))) > 1 , test_list))
print ( "The Distinct data type rows : " + str (res))
|
Output
The original list is : [[4, 3, 1], ['gfg', 3, {4: 2}], [3, 1, 'jkl'], [9, (2, 3)]]
The Distinct data type rows : [['gfg', 3, {4: 2}], [3, 1, 'jkl'], [9, (2, 3)]]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 4: Using collections.defaultdict class from the collections module.
Approach:
- Initialize an empty list called res, which will be used to store the rows that have more than one distinct data type.
- Loop through each row in test_list using a for loop.
- For each row, initialize an empty set called seen_types, which will be used to keep track of the data types seen so far.
- Loop through each item in the row using another for loop.
- For each item, use the type() function to get its data type and store it in a variable called item_type.
- Check if item_type is already in seen_types using an if statement.
- If item_type is not in seen_types, add it to the set using seen_types.add(item_type).
- After processing all the items in the row, check if seen_types has more than one distinct data type using len(seen_types) > 1.
- If seen_types has more than one distinct data type, append the row to res using res.append(row).
- After processing all the rows, print the result using print(“The Distinct data type rows : ” + str(res)).
Example:
Python3
test_list = [[ 4 , 3 , 1 ], [ "gfg" , 3 , { 4 : 2 }], [ 3 , 1 , "jkl" ], [ 9 , ( 2 , 3 )]]
print ( "The original list is : " + str (test_list))
res = []
for row in test_list:
seen_types = set ()
for item in row:
item_type = type (item)
if item_type not in seen_types:
seen_types.add(item_type)
if len (seen_types) > 1 :
res.append(row)
print ( "The Distinct data type rows : " + str (res))
|
Output
The original list is : [[4, 3, 1], ['gfg', 3, {4: 2}], [3, 1, 'jkl'], [9, (2, 3)]]
The Distinct data type rows : [['gfg', 3, {4: 2}], [3, 1, 'jkl'], [9, (2, 3)]]
Time complexity: O(n * m), where n is the number of rows in the input list and m is the maximum number of elements in a row.
Auxiliary Space: O(m), because the seen_types set can store at most one instance of each data type in a row.
Method 5: Using set and isinstance()
Use the set() function to find unique data types in each row, and the isinstance() function to check if a value is an instance of a particular data type.
Step-by-step approach:
- Initialize an empty list res.
- Iterate over each sublist sub in test_list.
- Initialize an empty set types to store the unique data types in sub.
- Iterate over each element elem in sub.
- Check if type(elem) is already in types. If not, add it to types.
- If the length of types is greater than 1, it means there are multiple data types in sub. In that case, append sub to res.
- Return res.
Python3
test_list = [[ 4 , 3 , 1 ], [ "gfg" , 3 , { 4 : 2 }],
[ 3 , 1 , "jkl" ], [ 9 , ( 2 , 3 )]]
print ( "The original list is : " + str (test_list))
res = []
for sub in test_list:
types = set ()
for elem in sub:
types.add( type (elem))
if len (types) > 1 :
res.append(sub)
print ( "The Distinct data type rows : " + str (res))
|
Output
The original list is : [[4, 3, 1], ['gfg', 3, {4: 2}], [3, 1, 'jkl'], [9, (2, 3)]]
The Distinct data type rows : [['gfg', 3, {4: 2}], [3, 1, 'jkl'], [9, (2, 3)]]
Time complexity: O(nm), where n is the number of sublists in test_list and m is the maximum length of any sublist.
Auxiliary space: O(k), where k is the maximum number of unique data types in any sublist of test_list.
Similar Reads
Python Program to Extract Rows of a matrix with Even frequency Elements
Given a Matrix, the task is to write a Python program to extract all the rows which have even frequencies of elements. Examples: Input: [[4, 5, 5, 2], [4, 4, 4, 4, 2, 2], [6, 5, 6, 5], [1, 2, 3, 4]] Output: [[4, 4, 4, 4, 2, 2], [6, 5, 6, 5]]Explanation: frequency of 4-> 4 which is even frequency
5 min read
Python Program that prints the rows of a given length from a matrix
Given a Matrix, the following articles shows how to extract all the rows with a specified length. Input : test_list = [[3, 4, 5, 6], [1, 4, 6], [2], [2, 3, 4, 5, 6], [7, 3, 1]], K = 3 Output : [[1, 4, 6], [7, 3, 1]] Explanation : Extracted lists have length of 3.Input : test_list = [[3, 4, 5, 6], [1
4 min read
Python program to Convert a Matrix to Sparse Matrix
Converting a matrix to a sparse matrix involves storing only non-zero elements along with their row and column indices to save memory. Using a DictionaryConverting a matrix to a sparse matrix using a dictionary involves storing only the non-zero elements of the matrix, with their row and column indi
2 min read
Python Program to Construct n*m Matrix from List
We are given a list we need to construct a n*m matrix from that list. For example, a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] we need to construct a 3*4 matrix so that resultant output becomes [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] . Using List ComprehensionThis method uses list comprehensio
3 min read
Python Program that filters out non-empty rows of a matrix
Given Matrix, the following article shows how to filter all the Non-Empty rows of a matrix. In simpler terms, the codes provided below return a matrix after removing empty rows from it. Input : test_list = [[4, 5, 6, 7], [], [], [9, 8, 1], []] Output : [[4, 5, 6, 7], [9, 8, 1]] Explanation : All emp
4 min read
Python - Extract Particular data type rows
Given A Matrix, extract all the rows which have all the elements with particular data type. Input : test_list = [[4, 5, "Hello"], [2, 6, 7], ["g", "f", "g"], [9, 10, 11]], data_type = int Output : [[2, 6, 7], [9, 10, 11]] Explanation : All lists with integer are extracted. Input : test_list = [[4, 5
3 min read
Python Program to Convert Matrix to String
Program converts a 2D matrix (list of lists) into a single string, where all the matrix elements are arranged in row-major order. The elements are separated by spaces or any other delimiter, making it easy to represent matrix data as a string. Using List ComprehensionList comprehension provides a co
2 min read
Python program to extract rows with common difference elements
Given a Matrix, extract rows with AP sequence. Input : test_list = [[4, 7, 10], [8, 10, 12], [10, 11, 13], [6, 8, 10]] Output : [[4, 7, 10], [8, 10, 12], [6, 8, 10]] Explanation : 3, 4, and 2 are common difference in AP. Input : test_list = [[4, 7, 10], [8, 10, 13], [10, 11, 13], [6, 8, 10]] Output
3 min read
Python program to return rows that have element at a specified index
Given two Matrices, the task is to write a Python program that can extract all the rows from both matrices which have similar elements at their Kth index, mapped at similar row positions. Examples: Input : test_list1 = [[1, 8, 3], [9, 2, 0], [6, 4, 4], [6, 4, 4]], test_list2 = [[1, 9, 3], [8, 2, 3],
5 min read
Python program to print the dictionary in table format
Given a Dictionary. The task is to print the dictionary in table format. Examples: Input: {1: ["Samuel", 21, 'Data Structures'], 2: ["Richie", 20, 'Machine Learning'], 3: ["Lauren", 21, 'OOPS with java'], }Output: NAME AGE COURSE Samuel 21 Data Structures Richie 20 Machine Learning Lauren 21 OOPS wi
2 min read