Удалить элемент из списка, если содержимое того же символа? - PullRequest
0 голосов
/ 08 октября 2019

Я пытаюсь удалить все элементы из списка, которые содержат определенный символ, а именно "z", "l" или "t". Вот что у меня есть:

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))

Я ожидаю, что выходные данные этой функции вернут все слова, которые не содержат ['z', 'l', 't'], как это:

['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']

, но фактический вывод:

['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']

Также, если функция не находит какой-либо элемент с запрещенным символом, она застревает в цикле while.

Спасибо за помощь. : 0)

Ответы [ 3 ]

1 голос
/ 08 октября 2019

Вам просто нужно перебрать слова и использовать all () для условия if:

Проверить, существует ли несколько строк в другой строке

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))

Выход

['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']
0 голосов
/ 08 октября 2019

То, что у вас есть на данный момент, немного сложнее, вот другой подход:

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
0 голосов
/ 08 октября 2019

Вот гораздо более питонный способ достижения вашей цели.

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)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...