Sometimes, while working with Python tuples list, we can have a problem in which we need to perform retention of all the records where occurrences of K is N times. This kind of problem can come in domains such as web development and day-day programming. Let’s discuss certain ways in which this task can be performed.
Input : test_list = [(4, 5, 5, 4), (5, 4, 3)], K = 5, N = 2
Output : [(4, 5, 5, 4)]
Input : test_list = [(4, 5, 5, 4), (5, 4, 3)], K = 5, N = 3
Output : []
Method #1 : Using list comprehension + count()
The combination of above functions can be used to solve this problem. In this, we perform the task of counting occurrences and conditions and iterations using list comprehension.
Python3
test_list = [( 4 , 5 , 6 , 4 , 4 ), ( 4 , 4 , 3 ), ( 4 , 4 , 4 ), ( 3 , 4 , 9 )]
print ( "The original list is : " + str (test_list))
K = 4
N = 3
res = [ele for ele in test_list if ele.count(K) = = N]
print ( "Filtered tuples : " + str (res))
|
Output :
The original list is : [(4, 5, 6, 4, 4), (4, 4, 3), (4, 4, 4), (3, 4, 9)]
Filtered tuples : [(4, 5, 6, 4, 4), (4, 4, 4)]
Time Complexity: O(n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n) where n is the number of elements in the list “test_list”.
Method #2 : Using list comprehension + sum()
The combination of above functions can be used to solve this problem. In this, we perform the task of computing summation count of K using sum().
Python3
test_list = [( 4 , 5 , 6 , 4 , 4 ), ( 4 , 4 , 3 ), ( 4 , 4 , 4 ), ( 3 , 4 , 9 )]
print ( "The original list is : " + str (test_list))
K = 4
N = 3
res = [ele for ele in test_list if sum (cnt = = K for cnt in ele) = = N]
print ( "Filtered tuples : " + str (res))
|
Output :
The original list is : [(4, 5, 6, 4, 4), (4, 4, 3), (4, 4, 4), (3, 4, 9)]
Filtered tuples : [(4, 5, 6, 4, 4), (4, 4, 4)]
Time Complexity: O(n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n) where n is the number of elements in the list “test_list”.
Method#3:Using count() + Recursive function
Algorithm:
- Define the recursive function retain_records that takes a list of tuples, a value K, and a value N as input.
- If the list is empty, return an empty list.
- If the first tuple in the list has N occurrences of K, append it to the result list and call the function recursively with the rest of the list.
- If the first tuple in the list does not have N occurrences of K, skip it and call the function recursively with the rest of the list.
- Return the result list.
Python3
def retain_records(lst, k, n):
if not lst:
return []
else :
if lst[ 0 ].count(k) = = n:
return [lst[ 0 ]] + retain_records(lst[ 1 :], k, n)
else :
return retain_records(lst[ 1 :], k, n)
test_list = [( 4 , 5 , 6 , 4 , 4 ), ( 4 , 4 , 3 ), ( 4 , 4 , 4 ), ( 3 , 4 , 9 )]
print ( "The original list is : " + str (test_list))
K = 4
N = 3
res = retain_records(test_list,K,N)
print ( "Filtered tuples : " + str (res))
|
Output
The original list is : [(4, 5, 6, 4, 4), (4, 4, 3), (4, 4, 4), (3, 4, 9)]
Filtered tuples : [(4, 5, 6, 4, 4), (4, 4, 4)]
Time complexity: O(n), where n is the length of the input list. This is because the function recursively traverses the list once and performs a constant amount of work for each tuple.
Auxiliary space: O(n), where n is the length of the input list. This is because the function creates a new list to store the result tuples, which could be as large as the input list if all tuples meet the filter condition. The recursive call stack also uses O(n) space, as there could be n nested function calls in the worst case.
Method #4: Using operator.countOf() method
- Initiated a for loop to traverse the list
- Check whether the count of K is equal to N
- If yes append that tuple to output list
- Display output list
Python3
test_list = [( 4 , 5 , 6 , 4 , 4 ), ( 4 , 4 , 3 ), ( 4 , 4 , 4 ), ( 3 , 4 , 9 )]
print ( "The original list is : " + str (test_list))
K = 4
N = 3
res = []
import operator
for i in test_list:
if (operator.countOf(i,K) = = N):
res.append(i)
print ( "Filtered tuples : " + str (res))
|
Output
The original list is : [(4, 5, 6, 4, 4), (4, 4, 3), (4, 4, 4), (3, 4, 9)]
Filtered tuples : [(4, 5, 6, 4, 4), (4, 4, 4)]
Time Complexity : O(N)
Auxiliary Space : O(N)
Method #5 : Using filter() and lambda():
1.Define a list of tuples test_list.
2.Define the values of K and N.
3.Use the filter() function along with a lambda function to create a new list res of tuples that only contain tuples that have exactly N occurrences of the value K.
4.Convert the filter() object to a list using the list() function and assign it to the variable res.
5.Print out the filtered tuples as a string.
Python3
test_list = [( 4 , 5 , 6 , 4 , 4 ), ( 4 , 4 , 3 ), ( 4 , 4 , 4 ), ( 3 , 4 , 9 )]
K = 4
N = 3
print ( "The original list is : " + str (test_list))
res = list ( filter ( lambda x: x.count(K) = = N, test_list))
print ( "Filtered tuples : " + str (res))
|
Output
The original list is : [(4, 5, 6, 4, 4), (4, 4, 3), (4, 4, 4), (3, 4, 9)]
Filtered tuples : [(4, 5, 6, 4, 4), (4, 4, 4)]
Time complexity : O(n * m), where n is the length of the input list test_list and m is the maximum length of a tuple in the list. This is because the filter() function needs to iterate over every tuple in the list, and the count() method needs to iterate over every element in each tuple.
Auxiliary space : O(k), where k is the number of tuples that satisfy the filter condition. This is because the filter() function returns an iterator, and the list() function creates a new list to store the filtered tuples.
Method #6: Using for loop
Algorithm:
- Initialize a list of tuples (test_list), and integers K and N.
- Create an empty list (res) to store the filtered tuples.
- Loop through each tuple (tup) in test_list:
a. Check if the count of K in tup is equal to N.
b. If the condition is True, append the tuple to res.
- Print the filtered tuples (res).
Python3
test_list = [( 4 , 5 , 6 , 4 , 4 ), ( 4 , 4 , 3 ), ( 4 , 4 , 4 ), ( 3 , 4 , 9 )]
K = 4
N = 3
res = []
for tup in test_list:
if tup.count(K) = = N:
res.append(tup)
print ( "Filtered tuples : " + str (res))
|
Output
Filtered tuples : [(4, 5, 6, 4, 4), (4, 4, 4)]
Time Complexity: O(n * k), where n is the number of tuples in the list and k is the average length of each tuple. This is because we are looping through each tuple in the list and then looping through each element in the tuple to count the occurrences of K.
Auxiliary Space: O(n), where n is the number of tuples in the list. This is because we are storing the filtered tuples in a list (res) which can contain at most n elements.
Method #7: Using filter() and partial() from functools module
Use the filter() method along with the partial() method from the functools module to filter the tuples based on the given conditions.
Step-by-step approach:
- Import the functools module.
- Define a function, ‘count_k_in_tuple()‘, that takes in two parameters – k and tuple_t.
- Inside the function, use the count() method to count the number of times k occurs in tuple_t.
- If the count is equal to N, return True, else return False.
- Create a partial function ‘filter_func‘ using the partial() method, where the first argument is count_k_in_tuple() function and the second argument is K.
- Use the filter() method along with the filter_func and test_list to filter the tuples based on the given conditions.
- Convert the filtered tuples into a list and assign it to the variable ‘res‘.
- Print the result.
Below is the implementation of the above approach:
Python3
import functools
test_list = [( 4 , 5 , 6 , 4 , 4 ), ( 4 , 4 , 3 ), ( 4 , 4 , 4 ), ( 3 , 4 , 9 )]
K = 4
N = 3
def count_k_in_tuple(k, tuple_t):
count = tuple_t.count(k)
if count = = N:
return True
else :
return False
filter_func = functools.partial(count_k_in_tuple, K)
filtered_tuples = filter (filter_func, test_list)
res = list (filtered_tuples)
print ( "Filtered tuples : " + str (res))
|
Output
Filtered tuples : [(4, 5, 6, 4, 4), (4, 4, 4)]
Time complexity: O(N*M), where N is the number of tuples in the list and M is the maximum length of any tuple in the list.
Auxiliary space: O(1) – We are only creating a few variables and not using any additional data structures that depend on the size of the input.
Please Login to comment...