- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
Python â€“ Check Similar elements in Matrix rows
When it is required to check for similar elements in a matrix row, a method is defined that take a matrix as parameter. The map method is used to covert the matrix to a tuple. The matrix values are iterated over and if the frequency is greater than 1, it is displayed on the console.
Below is a demonstration of the same
from collections import Counter def find_dupes(my_matrix): my_matrix = map(tuple,my_matrix) freq_dict = Counter(my_matrix) for (row,freq) in freq_dict.items(): if freq>1: print (row) my_matrix = [[1, 1, 0, 1, 0, 1], [0, 0, 1, 0, 0, 1], [1, 0, 1, 1, 0, 0], [1, 1, 0, 1, 0, 1], [0, 0, 1, 0, 0, 1], [0, 0, 1, 0, 0, 1]] print("The matrix is :") print(my_matrix) print("The result is :") find_dupes(my_matrix)
The matrix is : [[1, 1, 0, 1, 0, 1], [0, 0, 1, 0, 0, 1], [1, 0, 1, 1, 0, 0], [1, 1, 0, 1, 0, 1], [0, 0, 1, 0, 0, 1], [0, 0, 1, 0, 0, 1]] The result is : (1, 1, 0, 1, 0, 1) (0, 0, 1, 0, 0, 1)
The required packages are imported.
A method named ‘find_dupes’ is defined that takes a matrix as a parameter.
The ‘map’ method is used to convert the matrix to a tuple.
The counter method is used to get the count of every value in the matrix.
This is stored in a dictionary.
The dictionary items are iterated over.
If the frequency of any element is greater than 1, it is displayed on the console.
Outside the method, a matrix (technically a list of list) is defined and is displayed on the console.
The method is called by passing the required parameter.
The result is displayed on the console.
- Related Articles
- Remove similar element rows in tuple Matrix in Python
- Find distinct elements common to all rows of a matrix in Python
- Interchange elements of the first and last rows in the matrix using Python
- Python â€“ Test if Rows have Similar frequency
- Python Program to Extract Rows of a matrix with Even frequency Elements
- Python â€“ Remove Rows for similar Kth column element
- Python Program to Sort Matrix Rows by summation of consecutive difference of elements
- Python â€“ Rows with K string in Matrix
- How can Tensorflow be used to sum specific elements/rows of a matrix in Python?
- Check if all rows of a matrix are circular rotations of each other in Python
- Program to check some elements in matrix forms a cycle or not in python
- Python â€“ List Elements Grouping in Matrix
- Find distinct elements common to all rows of a Matrix in C++
- Python â€“ Filter rows with required elements
- Python â€“ Filter Rows with Range Elements