
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
Pair Elements with Rear Elements in Matrix Row using Python
In some scenarios of programming, you might have faced a time where you need to pair each element of matrices in a row with the rear element, in other words the element which appears immediately after the number. So, in this article we will be looking at some methods and examples to pair the elements according to the conditions.
Matrix
These are powerful data structures used to represent collections of elements organized in rows and columns. Like matrix having n row and m column will be called as n * m matrix.
Here are some methods which we can perform to pair the elements of the matrix.
Nested Loop
This is a simple and primary method which comes in the most of the developer's mind. Here we can use nested loops to iterate over each row of the matrix. Within the inner loop, we iterate from the first element of the row to the second-to-last element, as there is no subsequent element after the last one. The curr and nxt variables store the current element and the element that follows it which is the rear element.
Example
mat= [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] def pair_with_rear(mat): for row in mat: for i in range(len(row) - 1): curr = row[i] nxt = row[i + 1] print(f"Pair: {curr} - {nxt}") pair_with_rear(mat)
List Comprehension
This method uses list comprehension to create a pair of elements. This comprehension traverses on each row in the matrix and in each row it pairs each element with the rear element using (row[i], row[i+1]) which is stored in the pair list.
Example
mat = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] def pair_with_rear(mat): pairs = [(row[i], row[i + 1]) for row in mat for i in range(len(row) - 1)] for pair in pairs: print(f"Pair: {pair[0]} - {pair[1]}") pair_with_rear(mat)
Output
Pair: 1 - 2 Pair: 2 - 3 Pair: 4 - 5 Pair: 5 - 6 Pair: 7 - 8 Pair: 8 - 9
List Compression and Zip Method
This method uses the zip() function in conjunction with the list comprehension to pair each row element. This function takes the element from each iteration (in the below program it is the current row and sliced row with the elements from the second position) and combines them into tuples which are getting stored in the pairs list.
Example
mat = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] def pair_with_rear(mat): pairs = [(curr, nxt) for row in mat for curr, nxt in zip(row, row[1:])] for pair in pairs: print(f"Pair: {pair[0]} - {pair[1]}") pair_with_rear(mat)
Pair: 1 - 2 Pair: 2 - 3 Pair: 4 - 5 Pair: 5 - 6 Pair: 7 - 8 Pair: 8 - 9
NumPy Library
We use the NumPy library for the efficient process of the matrix. Firstly, we flattened the matrix to convert it into the 1D array using np.array(mat).flatten(). Then using list comprehension we create pairs by iterating over the flattened array and pair each element with its rear or subsequent element.
Example
import numpy as np mat = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] def pair_with_rear(matrix): flattened_matrix = np.array(matrix).flatten() pairs = [(flattened_matrix[i], flattened_matrix[i + 1]) for i in range(len(flattened_matrix) - 1)] for pair in pairs: print(f"Pair: {pair[0]} - {pair[1]}") pair_with_rear(mat)
Output
Pair: 1 - 2 Pair: 2 - 3 Pair: 3 - 4 Pair: 4 - 5 Pair: 5 - 6 Pair: 6 - 7 Pair: 7 - 8 Pair: 8 - 9
Itertools Linerary
We use Itertools library to handle the pairing operations. Similar to the Numpy Library approach in this method, we also flatten the array into the 1D matrix and concatenate all the rows of the matrix into a single 1D iterable array. Then using the list comprehension, the pairs are created by iterating over the flatten list, where the current element is denoted by flatten_mat[i] and the next element is denoted by flatten_mat[i+1] which is stored in the pairs list.
Example
import itertools mat = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] def pair_with_rear(matrix): flattened_matrix = list(itertools.chain.from_iterable(matrix)) pairs = [(flattened_matrix[i], flattened_matrix[i + 1]) for i in range(len(flattened_matrix) - 1)] for pair in pairs: print(f"Pair: {pair[0]} - {pair[1]}") pair_with_rear(mat)
Output
Pair: 1 - 2 Pair: 2 - 3 Pair: 3 - 4 Pair: 4 - 5 Pair: 5 - 6 Pair: 6 - 7 Pair: 7 - 8 Pair: 8 - 9
So, you may choose any method which you feel comfortable with. Each of the methods provide the way to pair the elements with its rear/subsequent element.