Python Forum
Problem: Check if a list contains a word and then continue with the next word
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem: Check if a list contains a word and then continue with the next word
#3
Sounds like you are trying to filter out bad words and bad words only. If that is the case, regex is probably the way to go. I made the following snippet (warning: may content offensive material, but kinda need to do it to illustrate my point):

import re

string = ['assassin', 'title','titassembled', 'ass', 'tit', 'asstit', 'hello']

def find_bad_word(string):
        bad_word_list = ['ass', 'tit']
        bad_iter = iter(bad_word_list)
        badword = next(bad_iter)
        cleaned = re.sub(badword, '', string)

        if len(cleaned) == 0 or cleaned == badword:
            return 'found bad word'

        while len(cleaned) != 0 or badword in cleaned:
            try:
                badword = next(bad_iter)
                cleaned = re.sub(badword, '', cleaned)
                if len(cleaned) == 0 or cleaned == badword:
                    return 'found bad word'
                    break

            except StopIteration:
                return string


for i in string:
    print(find_bad_word(i))
The above will print:
Output:
assassin title titassembled found bad word found bad word found bad word hello
I am sure there is a more elegant way to do it but its past midnight now so yeah.....

The above example assumes phrases like 'hellwtf' constitutes bad words and needs to be filtered out, whereas 'hellwtfs' is okay.
Reply


Messages In This Thread
RE: Problem: Check if a list contains a word and then continue with the next word - by palladium - Aug-12-2021, 04:25 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with to check an Input list data with a data read from an external source sacharyya 3 452 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  Retrieve word from string knob 4 530 Jan-22-2024, 06:40 PM
Last Post: Pedroski55
  [solved] list content check paul18fr 6 763 Jan-04-2024, 11:32 AM
Last Post: deanhystad
  How to create a table with different sizes of columns in MS word pepe 8 1,669 Dec-08-2023, 07:31 PM
Last Post: Pedroski55
  extract substring from a string before a word !! evilcode1 3 577 Nov-08-2023, 12:18 AM
Last Post: evilcode1
  Replace a text/word in docx file using Python Devan 4 3,614 Oct-17-2023, 06:03 PM
Last Post: Devan
  How to summarize an article that is stored in a word document on your laptop? Mikedicenso87 2 697 Oct-06-2023, 12:07 PM
Last Post: Mikedicenso87
Thumbs Up Convert word into pdf and copy table to outlook body in a prescribed format email2kmahe 1 791 Sep-22-2023, 02:33 PM
Last Post: carecavoador
  Guess the word game help jackthechampion 3 3,068 Sep-06-2023, 06:51 AM
Last Post: Pedroski55
  Automate Word snippets PHbench 0 573 Jun-06-2023, 06:59 PM
Last Post: PHbench

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020