Estoy tratando de eliminar todos los elementos de una lista que contienen un carácter particular, a saber, "z", "l" o "t". Esto es lo que tengo hasta ahora:
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))
Espero que el resultado de esta función devuelva todas las palabras que no contengan ['z', 'l', 't'] así:
['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']
Pero el resultado real es:
['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']
Además, si la función no encuentra ningún elemento que tenga un carácter prohibido, se queda atascado en el bucle while.
Gracias por la ayuda. : 0)
3 respuestas
Solo necesita recorrer las palabras y usar all () para la condición if:
Compruebe si existen varias cadenas en otra cadena
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))
Salida
['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']
Aquí hay una forma mucho más pitónica de lograr su objetivo.
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)
Lo que tienes por el momento es un poco demasiado complicado, aquí hay un enfoque diferente:
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
Preguntas relacionadas
Preguntas vinculadas
Nuevas preguntas
python
Python es un lenguaje de programación multipropósito, de tipificación dinámica y de múltiples paradigmas. Está diseñado para ser rápido de aprender, comprender y usar, y hacer cumplir una sintaxis limpia y uniforme. Tenga en cuenta que Python 2 está oficialmente fuera de soporte a partir del 01-01-2020. Aún así, para preguntas de Python específicas de la versión, agregue la etiqueta [python-2.7] o [python-3.x]. Cuando utilice una variante de Python (por ejemplo, Jython, PyPy) o una biblioteca (por ejemplo, Pandas y NumPy), inclúyala en las etiquetas.