Home > Net >  How to remove a string from brackets including the brackets with a condition?
How to remove a string from brackets including the brackets with a condition?

Time:05-25

I have a question similar to this one.

I want to remove brackets and the text within, but keep some words. For example I have a list:

["Going", "(#)", "(maybe)", "to", "the", "(##)", "(mall)", "market"]

I want to keep (#) and (##), but remove (maybe) and (mall).

Expected output:

["Going", "(#)", "to", "the", "(##)", "market"]

There can be any number of words with brackets in the list just like 'maybe' and 'mall'.

Brackets with # can have maximum of 3 hash.

CodePudding user response:

You can use a list-comprehension to filter the original list using a regular expression:

import re

a = ["Going", "(#)", "(maybe)", "to", "the", "(##)", "(mall)", "market"]
b = [word for word in a if re.match(r"[^(]|\(#{1,3}\)", word)]

Gives:

['Going', '(#)', 'to', 'the', '(##)', 'market']

re.match matches the pattern from the beginning of the string. The pattern means:

  • [^(] - any character except (.
  • | - or...
    • \( - literal parenthesis
    • #{1,3} - 1 to 3 repetitions of #
    • \) - literal parenthesis

Regex demo

CodePudding user response:

In a generic way you can parse the list and assess if the word has a pair of brackets. If it does, and if the word inside is not #, ## or ###, then you should exclude it from the output. Assuming you have a list of strings:

a = ['Going', '(#)', '(maybe)', 'to', 'the', '(##)', '(mall)', 'market']

output = [word for word in a if ('(' not in word and ')' not in word) or word.strip('()') in ['#', '##', '###']]
print(output)
# ['Going', '(#)', 'to', 'the', '(##)', 'market']

The strip method keeps only the string within the given parameters (in this case ( and )).

You can learn more about list comprehensions here: https://www.w3schools.com/python/python_lists_comprehension.asp

  • Related