
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print Rows Where All Elements' Frequency is Greater Than K in Python
When it is required to print rows where all its elements’ frequency is greater than K, a method is defined that takes two parameters, and uses ‘all’ operator and iteration to give the result.
Below is a demonstration of the same −
Example
def frequency_greater_K(row, K) : return all(row.count(element) > K for element in row) my_list = [[11, 11, 32, 43, 12, 23], [42, 14, 55, 62, 16], [11, 11, 11, 11], [42, 54, 61, 18]] print("The tuple is :") print(my_list) K = 1 print("The value of K is :") print(K) my_result = [row for row in my_list if frequency_greater_K(row, K)] print("The result is :") print(my_result)
Output
The tuple is : [[11, 11, 32, 43, 12, 23], [42, 14, 55, 62, 16], [11, 11, 11, 11], [42, 54, 61, 18]] The value of K is : 1 The result is : [[11, 11, 11, 11]]
Explanation
A method named ‘frequency_greater_K’ is defined that takes row and K value as parameters, and returns the comparison between element count and key as output.
A list of list is defined and displayed on the console.
A list comprehension is used to iterate over the list, and the method is called on every list.
This result is assigned to a variable.
This is the output that is displayed on the console.
Advertisements