-3

I have a list like this,

['Therefore', 'allowance' ,'(#)', 't(o)o', 'perfectly', 'gentleman', '(##)' ,'su(p)posing', 'man', 'his', 'now']

Expected output:

['Therefore', 'allowance' ,'(#)', 'too', 'perfectly', 'gentleman', '(##)' ,'supposing', 'man', 'his', 'now']

Removing the brackets is easy by using .replace(), but I don't want to remove the brackets from strings (#) and (##).

my code:

ch = "()"

for w in li:
    if w in ["(#)", "(##)"]:
        print(w)

    else:
        for c in ch:
            w.replace(c, "")
        print(w)

but this doesn't remove the brackets from the words.

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
Ben
  • 39
  • 6
  • 1
    Does this answer your question? [Why doesn't calling a string method do anything unless its output is assigned?](https://stackoverflow.com/questions/9189172/why-doesnt-calling-a-string-method-do-anything-unless-its-output-is-assigned) – Pranav Hosangadi May 25 '22 at 04:14
  • The question that you referred and the the question that is asked by me are totally different, please read it again, I have mentioned that if by just `.replace()` I cannot solve the problem it will remove all the brackets, I only want to remove the brackets from the words that fall under a certain condition. – Ben May 25 '22 at 05:55
  • No, they are the same. Your code fails because you only ever do `w.replace` but never assign its result. Please read https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ Knowing how to find out how your program fails to meet expectations is the first step to fixing it so that it meets expectations – Pranav Hosangadi May 25 '22 at 11:12
  • Please run the code, even if I don't assign a variable it will give me an answer, not in a list but separate words, if I append those words it will give me a list, but the answer was still wrong, but that was not the issue, the issue was that the condition did not work, your comment suggests that you never looked at the code properly in the first place. If you look at the correct answer I have chosen and my faulty code you will understand. – Ben May 25 '22 at 12:02
  • ... [try it online!](https://tio.run/##jVGxbsMgEN35ihMeDJLlpVukqMrQtUu7RR4QPsdIBBBgEX@9eziNMrXKLXDv3b13HGHNs3dv22YNHOHcfs8YcfIR2w5aZa0vymlsoWtFIyuWhZe@XgLGCXW2a00u6LLFq3I1EQ2VUkdaRJDBJ@MuFf5lZ5Pq4XxpB4a38KKtf9k0/G/JlM7Vc4AGTuOII2TiwRqHkD3oiCojKAd4DXklPGXG9EwtXEjOGE0JBYwj5sCAwkz3/MxpVt5RGY3ChztZgwx7FQK6URTJdhhtwmdBldRVQs9PsEYh19JHDFZpFJq0ufxLtoFPXyBE4zK9CK89sD0R/OMWaGc4HoCmo43LB3HSeVEWYCdI7kHsv3LcEVrS1xIJTji@b9sP) If that isn't your problem, you should include your _actual problem_ in your question, since Stack Overflow users have the significant handicap of not being omniscient or clairvoyant. – Pranav Hosangadi May 26 '22 at 02:23
  • If you tried the faulty code then the output should be there, a faulty one but output should generate, I have included my actual problem in the question, here is the output(wrong one) (#) (maybe) to the (##) (mall) market` – Ben May 26 '22 at 07:05
  • Those words are nowhere in your question. This is what I mean by adding enough detail to your question so that people can understand the problem. – Pranav Hosangadi May 26 '22 at 11:58
  • You can change the input if you want ,the task is not about THE WORDS but removing the brackets, my point is the output still shows without assigning the variable, that is the whole point. All the details ARE in the question that is why I got my answer from other people, the saw the problem and the understood it properly. – Ben May 27 '22 at 10:34

3 Answers3

1

You can use re.sub. In particular, note that it can take a function as repl parameter. The function takes a match object, and returns the desired replacement based on the information the match object has (e.g., m.group(1)).

import re

lst = ['Therefore', 'allowance', '(#)', 't(o)o', 'perfectly', 'gentleman', '(##)', 'su(p)posing', 'man', 'his', 'now']

def remove_paren(m):
    return m.group(0) if m.group(1) in ('#', '##') else m.group(1)

output = [re.sub(r"\((.*?)\)", remove_paren, word) for word in lst]
print(output) # ['Therefore', 'allowance', '(#)', 'too', 'perfectly', 'gentleman', '(##)', 'supposing', 'man', 'his', 'now']
j1-lee
  • 13,764
  • 3
  • 14
  • 26
  • You could just sub `\(([^#]*?)\)` with `\1` – Pranav Hosangadi May 25 '22 at 04:20
  • 1
    @PranavHosangadi I also considered that, but I wanted to handle cases like `(you're_my_#1)`. I know OP didn't ask for that, but I was just uncomfortable with that ;) – j1-lee May 25 '22 at 04:22
  • This works perfectly, I don't know much about `re` so I was hesitant to use it, but it is very handy. What if I want to remove strings from brackets including the brackets but keep (#) and (##), like this `['Therefore', 'allowance' ,'(#)', 'to', 'perfectly', 'gentleman', '(##)' ,'suposing', 'man', 'his', 'now']` . I removed 'o' and 'p' from 'too' and 'supposing'. – Ben May 25 '22 at 07:44
  • 1
    @Ben Try replacing `else m.group(1)` with `else ""`. – j1-lee May 25 '22 at 15:40
0

Give this a try!

Here, I define another empty array. And by looping in the original array to append the words again except the ones that we don't need.

At first, as you can see we got two loops. In the second one, we loop through each character and whenever we encounter a ( or ) we skip it and continue appending our string word.

If you notice that; to keep the (#) and (##) we skip the second loop but do not forget to add them again to the new list.

li = ["Therefore", "allowance", "(#)", "t(o)o" , "perfectly", "gentleman", "(##)", "su(p)posing", "man", "his", "now"]

new_li = []

for index, w in enumerate(li):
    if w in ["(#)", "(##)"]:
        new_li.append(w)
        continue 
    new_word = ""
    for c in w:
        if c == "(" or c == ")":
           continue
        new_word = new_word + c
    new_li.append(new_word)    

print(new_li)
0
def removeparanthesis(s): 
    a=''
    for i in s: 
        if i not in '()': 
            a+=i 
    return a
    
a = ['Therefore', 'allowance' , '(#)' , 't(o)o' , 'perfectly' , 'gentleman' , '(##)' , 'su(p)posing', 'man', 'his', 'now'] 

b=[]  

for i in a: 
    if i == '(#)' or i == '(##)':  
        b.append(i) 
    else: 
        b.append(removeparanthesis(i))
print(b) 

#I just created a function to remove parenthesis to those with not having them as a start and end