- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python – Test if Rows have Similar frequency
When it is required to check if rows have similar frequency, the ‘all’ operator, the ‘Counter’ method and a simple iteration is used.
Below is a demonstration of the same −
from collections import Counter my_list = [[21, 92, 64, 11, 3], [21, 3, 11, 92, 64], [64, 92, 21, 3, 11]] print("The list is :") print(my_list) my_result = all(dict(Counter(row)) == dict(Counter(my_list)) for row in my_list ) if(my_result == True): print("All rows have similar frequency") else: print("All rows do not have similar frequency")
The list is : [[21, 92, 64, 11, 3], [21, 3, 11, 92, 64], [64, 92, 21, 3, 11]] All rows have similar frequency
The required packages are imported into the environment.
A list of list with integers is defined and is displayed on the console.
The list in the list of list is converted to a Counter and then to a dictionary.
It is checked to see if elements in every list occur at the same frequency.
If yes, a Boolean value is stored in a variable.
Depending on this Boolean variable, relevant message is displayed on the console.
- Python – Check Similar elements in Matrix rows
- Python - Sort rows by Frequency of K
- Python – Test if all rows contain any common element with other Matrix
- Remove similar element rows in tuple Matrix in Python
- Python – Remove Rows for similar Kth column element
- Join Tuples if similar initial element in Python
- Test if Tuple contains K in Python
- Test if tuple is distinct in Python
- Python – Test if list is Palindrome
- Does MySQL have an expanded output flag similar PostgreSQL?
- Checking if starting digits are similar in list in Python
- Python Pandas - Check whether the two Index objects have similar object attributes and types
- Test whether similar int type of different sizes are subdtypes of integer class in Python
- Test whether similar float type of different sizes are subdtypes of floating class in Python
- Python Program to Extract Rows of a matrix with Even frequency Elements