Saturday, November 16, 2024
Google search engine
HomeLanguagesPython – Remove words containing list characters

Python – Remove words containing list characters

Sometimes, in the process of data filtering we have a problem in which we need to remove words which are composite of certain letters. This kind of application is common in data science domain. Let’s discuss certain ways in which this task can be performed. 

Method #1 : Using all() + list comprehension The combination of above methods can be used to perform this task. In this, we just check for all list characters using all() in each list and filters out string which has any one of characters. 

Python3




# Python3 code to demonstrate
# Remove words containing list characters
# using list comprehension + all()
from itertools import groupby
 
# initializing list
test_list = ['gfg', 'is', 'best', 'for', 'Lazyroar']
 
# initializing char list
char_list = ['g', 'o']
 
# printing original list
print ("The original list is : " + str(test_list))
 
# printing character list
print ("The character list is : " + str(char_list))
 
# Remove words containing list characters
# using list comprehension + all()
res = [ele for ele in test_list if all(ch not in ele for ch in char_list)]
 
# printing result
print ("The filtered strings are : " + str(res))


Output

The original list is : ['gfg', 'is', 'best', 'for', 'Lazyroar']
The character list is : ['g', 'o']
The filtered strings are : ['is', 'best']

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 new res list 

  Method #2 : Using loop This is brute method in which this task can be performed. In this we use loop and conditional statements to perform this task. 

Python3




# Python3 code to demonstrate
# Remove words containing list characters
# using loop
from itertools import groupby
 
# initializing list
test_list = ['gfg', 'is', 'best', 'for', 'Lazyroar']
 
# initializing char list
char_list = ['g', 'o']
 
# printing original list
print ("The original list is : " + str(test_list))
 
# printing character list
print ("The character list is : " + str(char_list))
 
# Remove words containing list characters
# using loop
res = []
flag = 1
for ele in test_list:
    for idx in char_list:
        if idx not in ele:
            flag = 1
        else:
            flag = 0
            break
    if(flag == 1):
        res.append(ele)
 
# printing result
print ("The filtered strings are : " + str(res))


Output

The original list is : ['gfg', 'is', 'best', 'for', 'Lazyroar']
The character list is : ['g', 'o']
The filtered strings are : ['is', 'best']

Time Complexity: O(n*n)
Auxiliary Space: O(n), where n is length of list.

Method #3 : Using replace() and len() methods

Python3




# Python3 code to demonstrate
# Remove words containing list characters
# initializing list
test_list = ['gfg', 'is', 'best', 'for', 'Lazyroar']
 
# initializing char list
char_list = ['g', 'o']
 
# printing original list
print ("The original list is : " + str(test_list))
 
# printing character list
print ("The character list is : " + str(char_list))
 
# Remove words containing list characters
 
res=[]
for i in test_list:
    x=i
    for j in char_list:
       i=i.replace(j,"")
    if(len(i)==len(x)):
        res.append(i)
# printing result
print ("The filtered strings are : " + str(res))


Output

The original list is : ['gfg', 'is', 'best', 'for', 'Lazyroar']
The character list is : ['g', 'o']
The filtered strings are : ['is', 'best']

Time Complexity : O(N*N)
Auxiliary space : O(1)

Method #4 : Using Counter() function

Python3




# Python3 code to demonstrate
# Remove words containing list characters
from collections import Counter
test_list = ['gfg', 'is', 'best', 'for', 'Lazyroar']
 
# initializing char list
char_list = ['g', 'o']
 
# printing original list
print("The original list is : " + str(test_list))
 
# printing character list
print("The character list is : " + str(char_list))
 
# Remove words containing list characters
 
res = []
freqCharList = Counter(char_list)
for i in test_list:
    unique = True
    for char in i:
      if char in freqCharList.keys():
        unique =False
        break
    if(unique):
      res.append(i)
       
       
# printing result
print ("The filtered strings are : " + str(res))


Output

The original list is : ['gfg', 'is', 'best', 'for', 'Lazyroar']
The character list is : ['g', 'o']
The filtered strings are : ['is', 'best']

Time Complexity : O(N*N)
Auxiliary space : O(N)

Method #5: Using operator.countOf() method

Python3




# Python3 code to demonstrate
# Remove words containing list characters
from itertools import groupby
import operator as op
# initializing list
test_list = ['gfg', 'is', 'best', 'for', 'Lazyroar']
 
# initializing char list
char_list = ['g', 'o']
 
# printing original list
print("The original list is : " + str(test_list))
 
# printing character list
print("The character list is : " + str(char_list))
 
# Remove words containing list characters
# using loop
res = []
flag = 1
for ele in test_list:
    for idx in char_list:
        if op.countOf(ele, idx) == 0:
            flag = 1
        else:
            flag = 0
            break
    if(flag == 1):
        res.append(ele)
 
# printing result
print("The filtered strings are : " + str(res))


Output

The original list is : ['gfg', 'is', 'best', 'for', 'Lazyroar']
The character list is : ['g', 'o']
The filtered strings are : ['is', 'best']

Time Complexity : O(N*N)
Auxiliary space : O(1)

Method #6: Using the recursive method.

Python3




# Python3 code to demonstrate
# Remove words containing list characters
 
def remove_words(start,lst,charlst,newlist=[]):
  if start==len(lst):
    return newlist
  flag=0
  for i in charlst:
    if in lst[start]:
      flag=1
  if flag==0:
     newlist.append(lst[start])
  return remove_words(start+1,lst,charlst,newlist)
# initializing list
test_list = ['gfg', 'is', 'best', 'for', 'Lazyroar']
 
# initializing char list
char_list = ['g', 'o']
 
# printing original list
print("The original list is : " + str(test_list))
 
# printing character list
print("The character list is : " + str(char_list))
 
# Remove words containing list characters
# using recursive function
res = remove_words(0,test_list,char_list)
 
# printing result
print("The filtered strings are : " + str(res))
#this code contributed by tvsk


Output

The original list is : ['gfg', 'is', 'best', 'for', 'Lazyroar']
The character list is : ['g', 'o']
The filtered strings are : ['is', 'best']

Time Complexity : O(N*N)
Auxiliary space : O(N)

Method #7: Using filter() and lambda function:

Python3




test_list = ['gfg', 'is', 'best', 'for', 'Lazyroar']
char_list = ['g', 'o']
# printing original list
print("The original list is : " + str(test_list))
  
# printing character list
print("The character list is : " + str(char_list))
res = list(filter(lambda x: not any(y in x for y in char_list), test_list))
print(res)


Output

The original list is : ['gfg', 'is', 'best', 'for', 'Lazyroar']
The character list is : ['g', 'o']
['is', 'best']

Time Complexity : O(N*N)
Auxiliary space : O(N)

Method #8 : Using set intersection

Python3




# Python3 code to demonstrate
# Remove words containing list characters
 
# initializing list
test_list = ['gfg', 'is', 'best', 'for', 'Lazyroar']
 
# initializing char list
char_list = set(['g', 'o'])
 
# printing original list
print("The original list is : " + str(test_list))
 
# printing character list
print("The character list is : " + str(char_list))
 
# Remove words containing list characters
res = list(filter(lambda x: not set(x).intersection(char_list), test_list))
 
# printing result
print("The filtered strings are : " + str(res))


Output

The original list is : ['gfg', 'is', 'best', 'for', 'Lazyroar']
The character list is : {'o', 'g'}
The filtered strings are : ['is', 'best']

Time complexity: O(N), where N is the number of elements in test_list
Auxiliary Space: O(N), in the worst case, all elements in test_list are kept in the resulting list

Explanation: This method uses filter and set to achieve the desired result. set is used to convert the list of characters into a set, which allows for constant time look ups. filter is used to keep only those elements from test_list for which the intersection with char_list is empty.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments