Given Matrix, extract all rows whose all elements have a frequency greater than K.
Input : test_list = [[1, 1, 2, 3, 2, 3], [4, 4, 5, 6, 6], [1, 1, 1, 1], [4, 5, 6, 8]], K = 2
Output : [[1, 1, 1, 1]]
Explanation : Here, frequency of 1 is 4 > 2, hence row retained.
Input : test_list = [[1, 1, 2, 3, 2, 3], [4, 4, 5, 6, 6], [1, 3, 4, 1], [4, 5, 6, 8]], K = 2
Output : []
Explanation : No list filtered as result.
Method #1 : Using list comprehension + all() + count()
In this, we perform task of iterating through elements using list comprehension and all() is used to check for each elements count(), extracted using count().
Python3
# Python3 code to demonstrate working of
# Rows with all Elements frequency greater than K
# Using list comprehension + count() + all()
def freq_greater_K(row, K) :
# checking for all elements occurrence greater than K
return all(row.count(ele) > K for ele in row)
# initializing list
test_list = [[1, 1, 2, 3, 2, 3], [4, 4, 5, 6, 6], [1, 1, 1, 1], [4, 5, 6, 8]]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 1
# checking for each row
res = [ele for ele in test_list if freq_greater_K(ele, K)]
# printing result
print("Filtered rows : " + str(res))
Output:
The original list is : [[1, 1, 2, 3, 2, 3], [4, 4, 5, 6, 6], [1, 1, 1, 1], [4, 5, 6, 8]] Filtered rows : [[1, 1, 2, 3, 2, 3], [1, 1, 1, 1]]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using filter() + lambda + all() + count()
In this, task of filtering is done using filter() + lambda, all() is used to check for each element, count() to compute count.
Python3
# Python3 code to demonstrate working of
# Rows with all Elements frequency greater than K
# Using filter() + lambda + all() + count()
def freq_greater_K(row, K) :
# checking for all elements occurrence greater than K
return all(row.count(ele) > K for ele in row)
# initializing list
test_list = [[1, 1, 2, 3, 2, 3], [4, 4, 5, 6, 6], [1, 1, 1, 1], [4, 5, 6, 8]]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 1
# checking for each row
res = list(filter(lambda ele : freq_greater_K(ele, K), test_list))
# printing result
print("Filtered rows : " + str(res))
Output:
The original list is : [[1, 1, 2, 3, 2, 3], [4, 4, 5, 6, 6], [1, 1, 1, 1], [4, 5, 6, 8]] Filtered rows : [[1, 1, 2, 3, 2, 3], [1, 1, 1, 1]]
Time Complexity: O(n*n) where n is the number of elements in the list “test_list”. filter() + lambda + all() + count() performs n*n number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in the list
Method #3: Using Counter() function
Python3
# Python3 code to demonstrate working of
# Rows with all Elements frequency greater than K
from collections import Counter
# initializing list
test_list = [[1, 1, 2, 3, 2, 3], [4, 4, 5, 6, 6], [1, 1, 1, 1], [4, 5, 6, 8]]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 1
for i in test_list.copy():
freq = Counter(i)
for key, value in freq.items():
if(value <= K):
test_list.remove(i)
break
# printing result
print("Filtered rows : " + str(test_list))
OutputThe original list is : [[1, 1, 2, 3, 2, 3], [4, 4, 5, 6, 6], [1, 1, 1, 1], [4, 5, 6, 8]]
Filtered rows : [[1, 1, 2, 3, 2, 3], [1, 1, 1, 1]]
Time Complexity: O(N*N)
Auxiliary Space : O(N)
Method 4: Using operator.countOf() function
Python3
import operator as op
# Python3 code to demonstrate working of
# Rows with all Elements frequency greater than K
# Using list comprehension + operator.countOf() + all()
def freq_greater_K(row, K) :
# checking for all elements occurrence greater than K
return all(op.countOf(row,ele) > K for ele in row)
# initializing list
test_list = [[1, 1, 2, 3, 2, 3], [4, 4, 5, 6, 6], [1, 1, 1, 1], [4, 5, 6, 8]]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 1
# checking for each row
res = [ele for ele in test_list if freq_greater_K(ele, K)]
# printing result
print("Filtered rows : " + str(res))
OutputThe original list is : [[1, 1, 2, 3, 2, 3], [4, 4, 5, 6, 6], [1, 1, 1, 1], [4, 5, 6, 8]]
Filtered rows : [[1, 1, 2, 3, 2, 3], [1, 1, 1, 1]]
Time Complexity: O(N*N)
Auxiliary Space : O(N)
Method 5: Using numpy:
Algorithm:
- Initialize a list of lists named "test_list" and a variable K with the given values.
- Filter the list "test_list" using list comprehension and check whether all the elements of each sublist occur more than K times or not.
- To check the frequency of each element of the sublist, we first convert the sublist into a numpy array using np.array() function.
Python3
import numpy as np
# initializing list
test_list = [[1, 1, 2, 3, 2, 3], [4, 4, 5, 6, 6], [1, 1, 1, 1], [4, 5, 6, 8]]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 1
# filtering list based on condition
res = [ele for ele in test_list if all(np.count_nonzero(np.array(ele) == x) > K for x in ele)]
# printing result
print("Filtered rows : " + str(res))
#This code is contributed by Jyothi pinjala.
Output:
The original list is : [[1, 1, 2, 3, 2, 3], [4, 4, 5, 6, 6], [1, 1, 1, 1], [4, 5, 6, 8]]
Filtered rows : [[1, 1, 2, 3, 2, 3], [1, 1, 1, 1]]
Time complexity:
The time complexity of the above code is O(n * m * k) where n is the number of rows, m is the maximum length of any row, and k is the value of K. Here, we are iterating over all the rows and then for each row, we are iterating over each element and then counting its frequency using np.count_nonzero() function. Therefore, the time complexity of this code is directly proportional to the size of the input.
Space complexity:
The space complexity of the above code is O(n * m) where n is the number of rows and m is the maximum length of any row. Here, we are creating a new list of filtered rows and storing the elements that pass the condition. Therefore, the space complexity of this code is directly proportional to the size of the input
Method #6 : Using a nested loop and a flag variable
Step-by-step approach:
- Initialize a list of lists, test_list, containing 4 sub-lists of integers.
- Set the value of K to 1.
- Initialize an empty list filtered_list to store the rows that have all elements with frequency greater than K.
- Iterate over each sub-list in test_list using a for loop.
- For each sub-list, set a flag variable flag to True.
- Iterate over each element in the sub-list using another for loop.
- Count the frequency of the current element in the sub-list using the count() method.
- If the frequency of the current element is less than or equal to K, set the flag to False and break out of the loop.
- If the flag is still True after checking all elements, add the sub-list to filtered_list.
- After iterating over all sub-lists, print the filtered_list to show the rows that have all elements with frequency greater than K.
Python3
test_list = [[1, 1, 2, 3, 2, 3], [4, 4, 5, 6, 6], [1, 1, 1, 1], [4, 5, 6, 8]]
K = 1
# initialize an empty list to store the filtered rows
filtered_list = []
# iterate over each row in the input list
for row in test_list:
# set a flag variable to True
flag = True
# iterate over each element in the row
for element in row:
# count the frequency of the element in the row
frequency = row.count(element)
# if the frequency is less than or equal to K, set the flag to False and break out of the loop
if frequency <= K:
flag = False
break
# if the flag is still True after checking all elements, add the row to the filtered list
if flag:
filtered_list.append(row)
# print the filtered list
print("Filtered rows:", filtered_list)
OutputFiltered rows: [[1, 1, 2, 3, 2, 3], [1, 1, 1, 1]]
Time complexity: O(n^2), where n is the total number of elements in the input list.
Auxiliary space: O(m), where m is the number of rows in the input list (used to store the filtered rows).
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 - Find the frequency of numbers greater than each element in a list Given a list, a new list is constructed that has frequency of elements greater than or equal to it, corresponding to each element of the list. Input : test_list = [6, 3, 7, 1, 2, 4] Output : [2, 4, 1, 6, 5, 3] Explanation : 6, 7 are greater or equal to 6 in list, hence 2. Input : test_list = [6, 3,
8 min read
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 - Extract elements with Frequency greater than K Given a List, extract all elements whose frequency is greater than K. Input : test_list = [4, 6, 4, 3, 3, 4, 3, 4, 3, 8], K = 3 Output : [4, 3] Explanation : Both elements occur 4 times. Input : test_list = [4, 6, 4, 3, 3, 4, 3, 4, 6, 6], K = 2 Output : [4, 3, 6] Explanation : Occur 4, 3, and 3 time
7 min read