Given a strings List, write a Python program that extracts all the strings with more than one occurrence of a specific value(here described using K) in elements of a list.
Examples:
Input : test_list = ["neveropen", "best", "for", "neveropen"], K = 'e' Output : ['neveropen', 'neveropen'] Explanation :neveropen andneveropen have 2 and 4 occurrences of K respectively.
Input : test_list = ["neveropen", "best", "for", "neveropen"], K = 'k' Output : ['neveropen'] Explanation :neveropen has 2 occurrences of K.
Method 1: Using loop and count()
In this, we check for all the occurrences of K in each string using count, and check if any string has more than 1 occurrence of K, and if found extract that string.
Python3
# Initializing Matrix test_list = [ "neveropen" , "best" , "for" , "neveropen" ] # Printing original list print ( "The original list is : " + str (test_list)) # Initializing K K = 'e' # Empty list res = [] for ele in test_list: # Checking for count greater than 1 (repetitive) if ele.count(K) > 1 : res.append(ele) # Printing result print ( "Repeated K strings : " + str (res)) |
The original list is : ['neveropen', 'best', 'for', 'neveropen'] Repeated K strings : ['neveropen', 'neveropen']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 2 : Using list comprehension and count()
This is short-hand solution for this task, similar to the above method, just iteration using is done using list comprehension.
Python3
# Initializing Matrix test_list = [ "neveropen" , "best" , "for" , "neveropen" ] # Printing original list print ( "The original list is : " + str (test_list)) # Initializing K K = 'e' # Checking for count greater than 1 (repetitive) # one-liner using list comprehension res = [ele for ele in test_list if ele.count(K) > 1 ] # Printing result print ( "Repeated K strings : " + str (res)) |
The original list is : ['neveropen', 'best', 'for', 'neveropen'] Repeated K strings : ['neveropen', 'neveropen']
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 replace() and len() methods
Python3
# Python strings with repetitive occurrence of an element in list # Initializing Matrix test_list = [ "neveropen" , "best" , "for" , "neveropen" ] # Printing original list print ( "The original list is : " + str (test_list)) # Initializing K K = 'e' # Empty list res = [] for ele in test_list: a = ele b = ele.replace(K, "") if ( len (a) - len (b) > = 2 ): res.append(ele) # Printing result print ( "Repeated K strings : " + str (res)) |
The original list is : ['neveropen', 'best', 'for', 'neveropen'] Repeated K strings : ['neveropen', 'neveropen']
Method 4: Using a regular expression.
- Import the re module.
- Define the regular expression pattern that matches strings with two or more occurrences of the character K.
- Use the filter() function to apply the regular expression pattern to each string in test_list and return only the strings that match.
- Converting the filtered result to a list and printing it.
Python3
import re # initializing Matrix test_list = [ "neveropen" , "best" , "for" , "neveropen" ] # printing original list print ( "The original list is : " + str (test_list)) # initializing K K = 'e' # defining regular expression pattern pattern = re. compile (f ".*{K}.*{K}.*" ) # applying regular expression pattern to each string in test_list res = list ( filter (pattern.match, test_list)) # printing result print ( "Repeated K strings : " + str (res)) |
The original list is : ['neveropen', 'best', 'for', 'neveropen'] Repeated K strings : ['neveropen', 'neveropen']
Time complexity: O(n), where n is the number of strings in test_list.
Auxiliary space: O(k), where k is the number of strings in test_list that match the regular expression pattern.
Method 5: Using the filter function.
- We start by initializing a list of strings called test_list. The list contains four strings: “neveropen”, “best”, “for”, and “neveropen”.
- We then print the original list using the print() function.
- We initialize a variable K with the value “e”. This is the character that we want to search for in the strings.
- We define a filter function called filter_func using a lambda function. The lambda function takes one argument element which represents an element of the list. The function returns True if the count of the character K in the element is greater than 1. Otherwise, it returns False.
- We use the filter() function to filter out the elements of the list that satisfy the condition specified by the filter function. The filter() function takes two arguments: the filter function and the list to be filtered. We convert the filtered output to a list using the list() function.
- Finally, we print the resulting list of strings that contain the character K more than once using the print() function.
Python3
# initializing Matrix test_list = [ "neveropen" , "best" , "for" , "neveropen" ] # printing original list print ( "The original list is : " + str (test_list)) # initializing K K = 'e' # defining filter function filter_func = lambda ele: ele.count(K) > 1 # filtering the list res = list ( filter (filter_func, test_list)) # printing result print ( "Repeated K strings : " + str (res)) |
The original list is : ['neveropen', 'best', 'for', 'neveropen'] Repeated K strings : ['neveropen', 'neveropen']
Time complexity: O(n), where n is the length of the list
Auxiliary space: O(k), where k is the number of strings that contain the character K more than once.