-3

I'm trying to remove all items from a list that contain a particular character, namely "z", "l", or "t". Here's what I have so far:

def run():
    words = ['the', 'of', 'and', 'to', 'a', 'in', 'for', 'is', 'on', 'that', 'by', 'this', 'with', 'i', 'you', 'it', 'not', 'or', 'be', 'are', 'from', 'at', 'as', 'your', 'all', 'have', 'new', 'more', 'an', 'was', 'we', 'will', 'home', 'can', 'us', 'about', 'if', 'page', 'my', 'has', 'search', 'free', 'but', 'our', 'one', 'other', 'do', 'no', 'information', 'time', 'they', 'site', 'he', 'up', 'may', 'what', 'which', 'their', 'news', 'out', 'use', 'any', 'there', 'see', 'only', 'so', 'his', 'when', 'contact', 'here', 'business', 'who', 'web', 'also', 'now', 'help', 'get', 'pm', 'view', 'online', 'c', 'e', 'first', 'am', 'been', 'would', 'how', 'were', 'me', 's', 'services', 'some', 'these', 'click', 'its', 'like', 'service', 'x', 'than', 'find', 'price', 'date', 'back', 'top', 'people', 'had', 'list', 'name', 'just', 'over', 'state', 'year', 'day', 'into', 'email', 'two', 'health', 'n', 'world', 're', 'next', 'used', 'go', 'b', 'work', 'last', 'most', 'products', 'music', 'buy', 'data', 'make', 'them', 'should', 'product', 'system', 'post', 'her', 'city', 't', 'add', 'policy', 'number', 'such', 'please', 'available', 'copyright', 'support', 'message', 'after', 'best', 'software', 'then', 'jan', 'good', 'video', 'well', 'd', 'where', 'info', 'rights', 'public', 'books', 'high', 'school', 'through', 'm', 'each', 'links', 'she', 'review', 'years', 'order', 'very', 'privacy', 'book', 'items', 'company', 'r', 'read', 'group', 'sex', 'need', 'many', 'user', 'said', 'de', 'does', 'set', 'under', 'general', 'research', 'university', 'january', 'mail', 'full', 'map', 'reviews', 'program', 'life', 'know', 'games', 'way', 'days', 'management', 'p', 'part', 'could', 'great', 'united', 'hotel', 'real', 'f', 'item', 'international', 'center', 'ebay', 'must', 'store', 'travel', 'comments', 'made', 'development', 'report', 'off', 'member', 'details', 'line', 'terms', 'before', 'hotels', 'did', 'send', 'right', 'type', 'because', 'local', 'those', 'using', 'results', 'office', 'education', 'national', 'car', 'design', 'take', 'posted', 'internet', 'address', 'community']
    f = ['z', 'l', 't']
    finished = False

    while finished == False:
        rep = False
        while rep == False:
            for x in range(len(words)):
                for filterWord in f:
                    try:
                        if filterWord in words[x]:
                            words.pop(x)
                            break
                    except Exception as e:
                        rep = True
            finished = True
    print(words)
    print(len(words))

I expect the output of this function to return all words that do not contain ['z', 'l', 't'] like this:

['of', 'and', 'a', 'in', 'for', 'is', 'on', 'by', 'i', 'you', 'or', 'be', 'are', 'from', 'as', 'your', 'have', 'new', 'more', 'an', 'was', 'we', 'home', 'can', 'us', 'if', 'page', 'my', 'has', 'search', 'free', 'our', 'one', 'do', 'no', 'he', 'up', 'may', 'which', 'news', 'use', 'any', 'see', 'so', 'his', 'when', 'here', 'business', 'who', 'web', 'now', 'pm', 'view', 'c', 'e', 'am', 'been', 'how', 'were', 'me', 's', 'services', 'some', 'service', 'x', 'find', 'price', 'back', 'had', 'name', 'over', 'year', 'day', 'n', 're', 'used', 'go', 'b', 'work', 'music', 'buy', 'make', 'her', 'add', 'number', 'such', 'message', 'jan', 'good', 'video', 'd', 'where', 'info', 'books', 'high', 'm', 'each', 'she', 'review', 'years', 'order', 'very', 'privacy', 'book', 'company', 'r', 'read', 'group', 'sex', 'need', 'many', 'user', 'said', 'de', 'does', 'under', 'research', 'january', 'map', 'reviews', 'program', 'know', 'games', 'way', 'days', 'p', 'f', 'ebay', 'made', 'off', 'member', 'before', 'did', 'send', 'because', 'using', 'office', 'car', 'design', 'address']

but the actual output is :

['of', 'and', 'a', 'in', 'for', 'is', 'on', 'by', 'i', 'you', 'or', 'be', 'are', 'from', 'as', 'your', 'have', 'new', 'more', 'an', 'was', 'we', 'home', 'can', 'us', 'if', 'page', 'my', 'has', 'search', 'free', 'our', 'one', 'do', 'no', 'he', 'up', 'may', 'which', 'news', 'use', 'any', 'see', 'so', 'his', 'when', 'here', 'business', 'who', 'web', 'now', 'pm', 'view', 'c', 'e', 'am', 'been', 'how', 'were', 'me', 's', 'services', 'some', 'service', 'x', 'find', 'price', 'back','had', 'name', 'over', 'year', 'day', 'n', 're', 'used', 'go', 'b', 'work','music', 'buy', 'make', 'her','add', 'number', 'such', 'message', 'jan', 'good', 'video', 'd', 'where', 'info', 'books', 'high', 'm', 'each', 'she', 'review', 'years', 'order', 'very', 'privacy', 'book', 'company', 'r', 'read', 'group', 'sex', 'need', 'many', 'user', 'said', 'de', 'does', 'under', 'research', 'january', 'map', 'reviews', 'program', 'know', 'games', 'way', 'days', 'p', 'f', 'ebay', 'comments', 'made', 'off', 'member', 'before', 'did', 'send', 'because', 'using', 'office', 'car', 'design', 'address']['of', 'and', 'a', 'in', 'for', 'is', 'on', 'by', 'with', 'i', 'you', 'not', 'or', 'be', 'are', 'from', 'as', 'your', 'have', 'new', 'more', 'an', 'was', 'we', 'home', 'can', 'us', 'if', 'page', 'my', 'has', 'search', 'free', 'our', 'one', 'do', 'no', 'time', 'site', 'he', 'up', 'may', 'which', 'news', 'use', 'any', 'see', 'so', 'his', 'when', 'here', 'business', 'who', 'web', 'now', 'get', 'pm', 'view', 'c', 'e', 'am', 'been', 'how', 'were', 'me', 's', 'services', 'some', 'click', 'like', 'service', 'x', 'find', 'price', 'back', 'people', 'had', 'name', 'over', 'year', 'day', 'email', 'health', 'n', 're', 'used', 'go', 'b', 'work', 'most', 'music', 'buy', 'make', 'should', 'system', 'her', 't', 'add', 'number', 'such', 'available', 'support', 'message', 'best', 'then', 'jan', 'good', 'video', 'd', 'where', 'info', 'public', 'books', 'high', 'through', 'm', 'each', 'she', 'review', 'years', 'order', 'very', 'privacy', 'book', 'company', 'r', 'read', 'group', 'sex', 'need', 'many', 'user', 'said', 'de', 'does', 'under', 'research', 'january', 'full', 'map', 'reviews', 'program', 'know', 'games', 'way', 'days', 'p', 'could', 'united', 'real', 'f', 'international', 'ebay', 'store', 'comments', 'made', 'report', 'off', 'member', 'line', 'before', 'did', 'send', 'type', 'because', 'those', 'using', 'office', 'national', 'car', 'design', 'posted', 'address']

Also if the function doesn't find any element that has a forbidden character, it gets stuck in the while loop.

Thanks for the help. :0)

NickD
  • 5,937
  • 1
  • 21
  • 38

3 Answers3

1

You just need to loop over the words and use all() for the if condition:

Check if multiple strings exist in another string

final_list = list()

words = ['the', 'of', 'and', 'to', 'a', 'in', 'for', 'is', 'on', 'that', 'by', 'this', 'with', 'i', 'you', 'it', 'not', 'or', 'be', 'are', 'from', 'at', 'as', 'your', 'all', 'have', 'new', 'more', 'an', 'was', 'we', 'will', 'home', 'can', 'us', 'about', 'if', 'page', 'my', 'has', 'search', 'free', 'but', 'our', 'one', 'other', 'do', 'no', 'information', 'time', 'they', 'site', 'he', 'up', 'may', 'what', 'which', 'their', 'news', 'out', 'use', 'any', 'there', 'see', 'only', 'so', 'his', 'when', 'contact', 'here', 'business', 'who', 'web', 'also', 'now', 'help', 'get', 'pm', 'view', 'online', 'c', 'e', 'first', 'am', 'been', 'would', 'how', 'were', 'me', 's', 'services', 'some', 'these', 'click', 'its', 'like', 'service', 'x', 'than', 'find', 'price', 'date', 'back', 'top', 'people', 'had', 'list', 'name', 'just', 'over', 'state', 'year', 'day', 'into', 'email', 'two', 'health', 'n', 'world', 're', 'next', 'used', 'go', 'b', 'work', 'last', 'most', 'products', 'music', 'buy', 'data', 'make', 'them', 'should', 'product', 'system', 'post', 'her', 'city', 't', 'add', 'policy', 'number', 'such', 'please', 'available', 'copyright', 'support', 'message', 'after', 'best', 'software', 'then', 'jan', 'good', 'video', 'well', 'd', 'where', 'info', 'rights', 'public', 'books', 'high', 'school', 'through', 'm', 'each', 'links', 'she', 'review', 'years', 'order', 'very', 'privacy', 'book', 'items', 'company', 'r', 'read', 'group', 'sex', 'need', 'many', 'user', 'said', 'de', 'does', 'set', 'under', 'general', 'research', 'university', 'january', 'mail', 'full', 'map', 'reviews', 'program', 'life', 'know', 'games', 'way', 'days', 'management', 'p', 'part', 'could', 'great', 'united', 'hotel', 'real', 'f', 'item', 'international', 'center', 'ebay', 'must', 'store', 'travel', 'comments', 'made', 'development', 'report', 'off', 'member', 'details', 'line', 'terms', 'before', 'hotels', 'did', 'send', 'right', 'type', 'because', 'local', 'those', 'using', 'results', 'office', 'education', 'national', 'car', 'design', 'take', 'posted', 'internet', 'address', 'community']

f = ['z', 'l', 't']

def run(words, not_allowed_f):
    for word in words:
        if all(x not in word for x in not_allowed_f):
            final_list.append(word)
    return final_list

print(run(words, f))

Output

['of', 'and', 'a', 'in', 'for', 'is', 'on', 'by', 'i', 'you', 'or', 'be', 'are', 'from', 'as', 'your', 'have', 'new', 'more', 'an', 'was', 'we', 'home', 'can', 'us', 'if', 'page', 'my', 'has', 'search', 'free', 'our', 'one', 'do', 'no', 'he', 'up', 'may', 'which', 'news', 'use', 'any', 'see', 'so', 'his', 'when', 'here', 'business', 'who', 'web', 'now', 'pm', 'view', 'c', 'e', 'am', 'been', 'how', 'were', 'me', 's', 'services', 'some', 'service', 'x', 'find', 'price', 'back', 'had', 'name', 'over', 'year', 'day', 'n', 're', 'used', 'go', 'b', 'work', 'music', 'buy', 'make', 'her', 'add', 'number', 'such', 'message', 'jan', 'good', 'video', 'd', 'where', 'info', 'books', 'high', 'm', 'each', 'she', 'review', 'years', 'order', 'very', 'privacy', 'book', 'company', 'r', 'read', 'group', 'sex', 'need', 'many', 'user', 'said', 'de', 'does', 'under', 'research', 'january', 'map', 'reviews', 'program', 'know', 'games', 'way', 'days', 'p', 'f', 'ebay', 'made', 'off', 'member', 'before', 'did', 'send', 'because', 'using', 'office', 'car', 'design', 'address']
Community
  • 1
  • 1
pissall
  • 7,109
  • 2
  • 25
  • 45
  • it didn't work! this what happened '''['the', 'the', 'of', 'of', 'of', 'and', 'and', 'and'...]''' +I replace this line '''for word in word:''' to this '''for word in words:''' and thanks – Mosa Salman Oct 08 '19 at 19:34
  • Why is my output different than yours? Can you try again? I have edited my answer once. Please upvote and select – pissall Oct 08 '19 at 19:35
0

Here's a much more pythonic way of achieving your goal.

def run():
    words = ['the', 'of', 'and', 'to', 'a', 'in', 'for', 'is', 'on', 'that', 'by', 'this', 'with', 'i', 'you', 'it', 'not', 'or', 'be', 'are', 'from', 'at', 'as', 'your', 'all', 'have', 'new', 'more', 'an', 'was', 'we', 'will', 'home', 'can', 'us', 'about', 'if', 'page', 'my', 'has', 'search', 'free', 'but', 'our', 'one', 'other', 'do', 'no', 'information', 'time', 'they', 'site', 'he', 'up', 'may', 'what', 'which', 'their', 'news', 'out', 'use', 'any', 'there', 'see', 'only', 'so', 'his', 'when', 'contact', 'here', 'business', 'who', 'web', 'also', 'now', 'help', 'get', 'pm', 'view', 'online', 'c', 'e', 'first', 'am', 'been', 'would', 'how', 'were', 'me', 's', 'services', 'some', 'these', 'click', 'its', 'like', 'service', 'x', 'than', 'find', 'price', 'date', 'back', 'top', 'people', 'had', 'list', 'name', 'just', 'over', 'state', 'year', 'day', 'into', 'email', 'two', 'health', 'n', 'world', 're', 'next', 'used', 'go', 'b', 'work', 'last', 'most', 'products', 'music', 'buy', 'data', 'make', 'them', 'should', 'product', 'system', 'post', 'her', 'city', 't', 'add', 'policy', 'number', 'such', 'please', 'available', 'copyright', 'support', 'message', 'after', 'best', 'software', 'then', 'jan', 'good', 'video', 'well', 'd', 'where', 'info', 'rights', 'public', 'books', 'high', 'school', 'through', 'm', 'each', 'links', 'she', 'review', 'years', 'order', 'very', 'privacy', 'book', 'items', 'company', 'r', 'read', 'group', 'sex', 'need', 'many', 'user', 'said', 'de', 'does', 'set', 'under', 'general', 'research', 'university', 'january', 'mail', 'full', 'map', 'reviews', 'program', 'life', 'know', 'games', 'way', 'days', 'management', 'p', 'part', 'could', 'great', 'united', 'hotel', 'real', 'f', 'item', 'international', 'center', 'ebay', 'must', 'store', 'travel', 'comments', 'made', 'development', 'report', 'off', 'member', 'details', 'line', 'terms', 'before', 'hotels', 'did', 'send', 'right', 'type', 'because', 'local', 'those', 'using', 'results', 'office', 'education', 'national', 'car', 'design', 'take', 'posted', 'internet', 'address', 'community']
    filter_letters = ['z', 'l', 't']
    filtered_words = []

    for word in words:
        valid_word = True
        for letter in filter_letters:
            if letter in word:
                valid_word = False
        if valid_word:
            filtered_words.append(word)

    print(filtered_words)
Josh Correia
  • 3,807
  • 3
  • 33
  • 50
0

What you have for the moment is a bit over-complicated, here is a different approach :

for i, word in enumerate(words): # iterates over the list with an index
    if any (f_word in word for f_word in f):
        # the a word contains a 'forbidden' string of the f list
        del words[i] # removes the element from the list
vgalin
  • 491
  • 4
  • 18