Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to Extract Rows of a matrix with Even frequency Elements
When working with matrices (lists of lists), we sometimes need to extract rows where each element appears an even number of times. This can be achieved using list comprehension with the all() function and Counter from the collections module.
Understanding Even Frequency
A row has "even frequency elements" when every unique element in that row appears an even number of times (2, 4, 6, etc.). For example:
[1, 1, 2, 2] − Element 1 appears 2 times, element 2 appears 2 times (both even)
[3, 3, 3, 4] − Element 3 appears 3 times (odd), so this row doesn't qualify
Example
Here's how to extract rows with even frequency elements ?
from collections import Counter
matrix = [[41, 25, 25, 62], [41, 41, 41, 41, 22, 22], [65, 57, 65, 57], [11, 24, 36, 48]]
print("Original matrix:")
print(matrix)
# Extract rows where all elements have even frequency
result = [row for row in matrix if all(count % 2 == 0 for count in Counter(row).values())]
print("\nRows with even frequency elements:")
print(result)
Original matrix: [[41, 25, 25, 62], [41, 41, 41, 41, 22, 22], [65, 57, 65, 57], [11, 24, 36, 48]] Rows with even frequency elements: [[41, 41, 41, 41, 22, 22], [65, 57, 65, 57]]
Step-by-Step Analysis
Let's examine each row to understand why certain rows are selected ?
from collections import Counter
matrix = [[41, 25, 25, 62], [41, 41, 41, 41, 22, 22], [65, 57, 65, 57], [11, 24, 36, 48]]
for i, row in enumerate(matrix):
freq = Counter(row)
print(f"Row {i}: {row}")
print(f" Frequencies: {dict(freq)}")
even_freq = all(count % 2 == 0 for count in freq.values())
print(f" All frequencies even? {even_freq}")
print()
Row 0: [41, 25, 25, 62]
Frequencies: {41: 1, 25: 2, 62: 1}
All frequencies even? False
Row 1: [41, 41, 41, 41, 22, 22]
Frequencies: {41: 4, 22: 2}
All frequencies even? True
Row 2: [65, 57, 65, 57]
Frequencies: {65: 2, 57: 2}
All frequencies even? True
Row 3: [11, 24, 36, 48]
Frequencies: {11: 1, 24: 1, 36: 1, 48: 1}
All frequencies even? False
How It Works
The solution uses these key components:
Counter(row) − Counts frequency of each element in the row
.values() − Gets just the count values (not the elements)
count % 2 == 0 − Checks if each count is even
all() − Returns True only if ALL counts are even
List comprehension − Filters rows that meet the condition
Alternative Approach Using Function
For better readability, you can define a helper function ?
from collections import Counter
def has_even_frequencies(row):
"""Check if all elements in row have even frequencies."""
return all(count % 2 == 0 for count in Counter(row).values())
matrix = [[1, 1, 2, 2], [3, 3, 3], [4, 4, 5, 5, 6, 6]]
result = [row for row in matrix if has_even_frequencies(row)]
print("Rows with even frequency elements:", result)
Rows with even frequency elements: [[1, 1, 2, 2], [4, 4, 5, 5, 6, 6]]
Conclusion
Use Counter to count element frequencies and all() with modulo to check if all frequencies are even. List comprehension provides an efficient way to filter matrix rows based on this condition.
