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
# Python3 code to demonstrate working of
# Distinct Data Type Rows
# Using type() + list comprehension
# Initializing list
test_list = [[4, 3, 1], ["gfg", 3, {4: 2}], [3, 1, "jkl"], [9, (2, 3)]]
# Printing original list
print("The original list is : " + str(test_list))
res = []
for sub in test_list:
# Getting the distinct types size
type_size = len(list(set([type(ele) for ele in sub])))
# If equal get result
if len(sub) == type_size:
res.append(sub)
# Printing the result
print("The Distinct data type rows : " + str(res))
OutputThe 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
# Python3 code to demonstrate working of
# Distinct Data Type Rows
# initializing list
test_list = [[4, 3, 1], ["gfg", 3, {4: 2}], [3, 1, "jkl"], [9, (2, 3)]]
# printing original list
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)
# printing result
print("The Distinct data type rows : " + str(res))
OutputThe 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
# Python3 code to demonstrate working of
# Distinct Data Type Rows
# Using reduce()
# initializing list
test_list = [[4, 3, 1], ["gfg", 3, {4: 2}],
[3, 1, "jkl"], [9, (2, 3)]]
# printing original list
print("The original list is : " + str(test_list))
# using reduce()
# extracting Distinct data type rows
res = list(filter(lambda sub: len(set(map(type, sub))) > 1, test_list))
# printing result
print("The Distinct data type rows : " + str(res))
OutputThe 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
# initializing list
test_list = [[4, 3, 1], ["gfg", 3, {4: 2}], [3, 1, "jkl"], [9, (2, 3)]]
# printing original list
print("The original list is : " + str(test_list))
# Extracting distinct data type rows
# using set
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)
# printing result
print("The Distinct data type rows : " + str(res))
OutputThe 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
# Python3 code to demonstrate working of
# Distinct Data Type Rows
# Using set and isinstance()
# initializing list
test_list = [[4, 3, 1], ["gfg", 3, {4: 2}],
[3, 1, "jkl"], [9, (2, 3)]]
# printing original list
print("The original list is : " + str(test_list))
# using set and isinstance()
# extracting Distinct data type rows
res = []
for sub in test_list:
types = set()
for elem in sub:
types.add(type(elem))
if len(types) > 1:
res.append(sub)
# printing result
print("The Distinct data type rows : " + str(res))
OutputThe 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 that prints rows from the matrix that have same element at a given index
Given a Matrix, the following article shows how rows which has similar digit at the specified index will be extracted and returned as output. Input : test_list = [[3345, 6355, 83, 938], [323, 923, 845], [192, 993, 49], [98, 34, 23]], K = 1 Output : [[3345, 6355, 83, 938], [192, 993, 49]] Explanation
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 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 remove rows with duplicate element in Matrix
Given Matrix, remove all rows which have duplicate elements in them. Input : test_list = [[4, 3, 2], [7, 6, 7], [2, 4, 4], [8, 9, 9]] Output : [[4, 3, 2]] Explanation : [4, 3, 2] is the only unique row. Input : test_list = [[4, 3, 3, 2], [7, 6, 7], [2, 4, 4], [8, 9, 9]] Output : [] Explanation : No
5 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 Convert String Matrix Representation to Matrix
Given a String with matrix representation, the task here is to write a python program that converts it to a matrix. Input : test_str = "[gfg,is],[best,for],[all,geeks]"Output : [['gfg', 'is'], ['best', 'for'], ['all', 'geeks']]Explanation : Required String Matrix is converted to Matrix with list as
4 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 with
2 min read
Python Program to Filter Rows with a specific Pair Sum
Given Matrix, the following program shows how to extract all rows which have a pair such that their sum is equal to a specific number, here denoted as K. Input : test_list = [[1, 5, 3, 6], [4, 3, 2, 1], [7, 2, 4, 5], [6, 9, 3, 2]], k = 8 Output : [[1, 5, 3, 6], [6, 9, 3, 2]] Explanation : 5 + 3 = 8
6 min read