Python program to extract rows with common difference elements
Last Updated :
28 Apr, 2023
Given a Matrix, extract rows with AP sequence.
Input : test_list = [[4, 7, 10], [8, 10, 12], [10, 11, 13], [6, 8, 10]]
Output : [[4, 7, 10], [8, 10, 12], [6, 8, 10]]
Explanation : 3, 4, and 2 are common difference in AP.
Input : test_list = [[4, 7, 10], [8, 10, 13], [10, 11, 13], [6, 8, 10]]
Output : [[4, 7, 10], [6, 8, 10]]
Explanation : 3 and 2 are common difference in AP.
Method #1: Using loop
In this, we check for all the elements having a common difference using a loop, if any element not found to be in sync, then the row is flagged off and not added to the result.
Python3
# Python3 code to demonstrate working of
# Extract rows with common difference elements
# Using loop
# initializing list
test_list = [[4, 7, 10],
[8, 10, 12],
[10, 11, 13],
[6, 8, 10]]
# printing original list
print("The original list is : " + str(test_list))
res = []
for row in test_list:
flag = True
for idx in range(0, len(row) - 1):
# check for common difference
if row[idx + 1] - row[idx] != row[1] - row[0]:
flag = False
break
if flag :
res.append(row)
# printing result
print("Filtered Matrix : " + str(res))
Output:
The original list is : [[4, 7, 10], [8, 10, 12], [10, 11, 13], [6, 8, 10]]
Filtered Matrix : [[4, 7, 10], [8, 10, 12], [6, 8, 10]]
Time Complexity: O(n*m)
Auxiliary Space: O(k)
Method #2: Using all() + list comprehension
In this, we check for all values to have common difference using all(), list comprehension is used to perform an iteration of rows.
Python3
# Python3 code to demonstrate working of
# all() + list comprehension
# Using list comprehension + all()
# initializing list
test_list = [[4, 7, 10],
[8, 10, 12],
[10, 11, 13],
[6, 8, 10]]
# printing original list
print("The original list is : " + str(test_list))
# checking for all values to have common difference
res = [row for row in test_list
if all(row[idx + 1] - row[idx] == row[1] - row[0]
for idx in range(0, len(row) - 1))]
# printing result
print("Filtered Matrix : " + str(res))
Output:
The original list is : [[4, 7, 10], [8, 10, 12], [10, 11, 13], [6, 8, 10]]
Filtered Matrix : [[4, 7, 10], [8, 10, 12], [6, 8, 10]]
Time Complexity: O(n*m)
Auxiliary Space: O(k)
Method 3 : using the NumPy library.
we can use the numpy.diff() function to find the differences between consecutive elements in each row, and then use numpy.all() to check if all elements in the resulting array are equal.
Python3
import numpy as np
# initializing list
test_list = [[4, 7, 10],
[8, 10, 12],
[10, 11, 13],
[6, 8, 10]]
# convert list to numpy array
arr = np.array(test_list)
# calculate differences between consecutive elements in each row
diffs = np.diff(arr, axis=1)
# check if all elements in diffs are equal
mask = np.all(diffs == diffs[:, 0][:, np.newaxis], axis=1)
# filter rows that meet the criteria
res = arr[mask].tolist()
# printing result
print("Filtered Matrix : " + str(res))
OUTPUT :
Filtered Matrix : [[4, 7, 10], [8, 10, 12], [6, 8, 10]]
The time complexity of this code is O(nm), where n is the number of rows and m is the number of columns in the input matrix.
The auxiliary space complexity of this code is O(nm), which includes the space required for the numpy array arr, the intermediate array diffs, the boolean mask mask, and the resulting filtered list res.
Similar Reads
Python - Extract element with relative difference greater than K Given a list of numbers, the task is to write a Python program to extract all numbers with differences of the next and previous elements with a current greater than K. Input : test_list = [2, 7, 4, 1, 9, 2, 3, 10, 1, 5], K = 4Output : [9, 10]Explanation : 9 has 1 as preceding element and 2 as succee
4 min read
Python program to remove rows with duplicate element in Matrix Given Matrix, remove all rows which have duplicate elements in them. Input : test_list = [[4, 3, 2], [7, 6, 7], [2, 4, 4], [8, 9, 9]] Output : [[4, 3, 2]] Explanation : [4, 3, 2] is the only unique row. Input : test_list = [[4, 3, 3, 2], [7, 6, 7], [2, 4, 4], [8, 9, 9]] Output : [] Explanation : No
5 min read
Python Program to Sort Matrix Rows by summation of consecutive difference of elements Given a Matrix, the following article depicts how to sort rows of a matrix on the basis of summation of difference between consecutive elements of a row. Input : test_list = [[1, 5, 3, 6], [4, 3, 2, 1], [7, 2, 4, 5], [6, 9, 3, 2]], Output : [[4, 3, 2, 1], [7, 2, 4, 5], [1, 5, 3, 6], [6, 9, 3, 2]] Ex
7 min read
Python - Dual Element row with Maximum difference Sometimes, while working with Python Matrix, we can have Matrix with its elements to be rows with just two elements and we may desire to get row with elements having maximum difference. This can have application in many domains. Lets discuss certain ways in which this task can be performed. Method #
6 min read
Python Program to get all possible differences between set elements Given a set, the task is to write a Python program to get all possible differences between its elements. Input : test_set = {1, 5, 2, 7, 3, 4, 10, 14} Output : {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13} Explanation : All possible differences are computed. Input : test_set = {1, 5, 2, 7} Output : {1
4 min read
Python - Extract elements with Range consecutive occurrences Sometimes while working with data, we can have a problem in which we need to select some of the elements that occur range times consecutively. This problem can occur in many domains. Letâs discuss certain ways in which this problem can be solved. Method #1 : Using groupby() + list comprehension This
4 min read