![]() |
VOOZH | about |
Sometimes, while working with Python Matrix, we can have a problem in which we need to check if an element is present or not. This problem is simpler, but a variation to it can be to perform a check in a particular column of Matrix. Let's discuss a shorthand by which this task can be performed.
Method 1: Using any() + list comprehension
This task can be performed using a combination of the above functions in which we imply list comprehension for iteration and support tasks and any() can be used to check for any occurrence of the desired character.
Step-by-step approach ;
Below is the implementation of the above approach:
The original list : [[1, 4, 5], [6, 7, 8], [8, 3, 0]] Does element exists in particular column : True
Time complexity: O(n), where n is the total number of elements in the matrix because we have to iterate through all the elements of the matrix to check if the element exists in the particular column.
Auxiliary space: O(1), because only a few variables are used and the memory usage does not depend on the size of the input.
Method 2: Using map() and lambda function:
Using the map() function along with a lambda function allows you to apply a specific operation to each element in an iterable such as a list or matrix. In this case, you can use the lambda function to check the Nth element of each row and return a list of boolean values indicating whether the element was present in that row or not. The map() function will apply the lambda function to each element in the matrix (rows), so the time complexity of this approach would be O(n) where n is the number of rows in the matrix. And Auxiliary space of this approach would be O(n) as well, as it creates a new list of booleans for each element in the matrix.
Here is an example of how to use the map() function along with a lambda function to check if an element is present in a particular column of a matrix:
The original list : [[1, 4, 5], [6, 7, 8], [8, 3, 0]] Does element exists in particular column : True
Time complexity: O(n), where n is the number of rows in the matrix (list of lists).
Auxiliary space: O(1), as it only uses a few constant-size variables (N, ele, res) and does not create any additional data structures.
Method 3: Using a for loop to iterate over each row and then accessing the element at the Nth column, comparing it with the desired element and setting a flag if found.
This program searches for a specific element in a particular column of a matrix represented as a list of lists. It initializes the matrix, the column to search (N), and the element to find (ele). It then uses a for loop to iterate through each row of the matrix and checks if the element exists in the Nth column of that row. If the element is found in the specified column, it sets the variable "found" to True and breaks out of the loop. Finally, it prints whether the element exists in the particular column or not.
The original list : [[1, 4, 5], [6, 7, 8], [8, 3, 0]] Does element exists in particular column : True
The time complexity of this method is O(n), where n is the number of rows in the matrix
The space complexity of this method is O(1), as we only use a constant amount of extra space to store the variables N, ele, and found.
Method 4: numpy library
step-by-step approach:
Output:
The original list : [[1 4 5] [6 7 8] [8 3 0]] Does element exists in particular column : True
Time complexity: O(n), where n is the number of elements in the matrix.
Auxiliary space: O(1)
Method 5: Using pandas library.
Step-by-step approach:
Output:
Does element exists in particular column : True
Time complexity: O(m*n), where m is the number of rows and n is the number of columns in the matrix.
Auxiliary space: O(m*n), as we create a DataFrame to store the matrix.
Method 6: Using the in-built function index()
Step-by-step approach:
Below is the implementation of the above approach:
The original list : [[1, 4, 5], [6, 7, 8], [8, 3, 0]] Does element exists in particular column : True
Time complexity: O(n*m) where n is the number of rows and m is the number of columns in test_list.
Auxiliary space: O(1) because it uses a constant amount of memory regardless of the size of test_list
Method 7: Using itertools:
The original list : [[1, 4, 5], [6, 7, 8], [8, 3, 0]] Does element exists in particular column : True
Time complexity: O(n^2) where n is the number of elements in the matrix. This is because itertools.chain() requires iterating over the entire matrix and then flattening it, and any() also requires iterating over the entire flattened list.
Space complexity: O(n) for the flattened list, since it requires creating a new list that contains all the elements of the matrix.