2

I have the following list

x = ['Accara building model (ABM)','tri-com model (tcm)']

Using re I was able to ignore the words in the parentheses. Like below

import re 
x = ['Accara building model (ABM)','tri-com model (tcm)']
for i in x:
    ko= list(re.sub("[\(\[].*?[\)\]]", "", i))
    print (ko)

but I get the output in the below format

['A', 'c', 'c', 'a', 'r', 'a', ' ', 'b', 'u', 'i', 'l', 'd', 'i', 'n', 'g', ' ', 'm', 'o', 'd', 'e', 'l', ' ']
['t', 'r', 'i', '-', 'c', 'o', 'm', ' ', 'm', 'o', 'd', 'e', 'l', ' ']

What I ideally want is like below in the fewest possible lines of code. (I know my code is currently inefficient)

Ideal output required

['Accara building model', 'tri-com model']
venkatttaknev
  • 669
  • 1
  • 7
  • 21
  • you get chars because you use `list()`. You should create empty list before loo and `append` results to this list. – furas Dec 01 '19 at 17:34

5 Answers5

3

You shouldn't use list() but you should create empty list before loop and append results to this list

import re

x = ['Accara building model (ABM)','tri-com model (tcm)']
results = []

for i in x:
    ko = re.sub("[\(\[].*?[\)\]]", "", i)
    resutls.append(ko.strip())

print(results)

Result

['Accara building model', 'tri-com model']

You can even use list comprehension

import re

x = ['Accara building model (ABM)','tri-com model (tcm)']

results = [re.sub("[\(\[].*?[\)\]]", "", i).strip() for i in x]

print(results)

BTW: I use strip() to remove space at the end. But you could remove this space with regex which starts with space " [\(\[].*?[\)\]]".


EDIT: as Mark Meyer suggested in comment you can also compile regex - so it will not have to do it in every loop.

x = ['Accara building model (ABM)','tri-com model (tcm)']

pattern = re.compile(" [\(\[].*?[\)\]]")
results = [re.sub(pattern, "", i) for i in x]

print(results)

BTW: if you are sure that elments will have always the same structure then you can remove it without regex but using split(' (')

x = ['Accara building model (ABM)','tri-com model (tcm)', 'name without parentheses']

results = [i.split(' (',1)[0] for i in x]

print(results)
furas
  • 134,197
  • 12
  • 106
  • 148
  • Thanks. I guess your last solution is the most efficient for my case as it involves less number of codes relatively. No extra creation of empty list and appending. – venkatttaknev Dec 01 '19 at 17:42
  • 2
    If efficiency is an issue, maybe compiling the re outside the loop would be a good idea. – Mark Dec 01 '19 at 17:43
0

You were almost there, try this :

import re
x = ['Accara building model (ABM)','tri-com model (tcm)']
output = []
for i in x:
    ko= re.sub("[\(\[].*?[\)\]]", "", i)
    output.append(ko)

Output : output list looks like

["Accara building model", "tri-com model"]

When you are using list(re.sub(...)) you are basically making the output string (after replacing) into a list format.

Arkistarvh Kltzuonstev
  • 6,824
  • 7
  • 26
  • 56
0
import re
x = ['Accara building model (ABM)','tri-com model (tcm)']

print([ "".join(list(re.sub("[\(\[].*?[\)\]]", "", i))) for i in x ])


python test
['Accara building model ', 'tri-com model ']
Ramon Medeiros
  • 2,272
  • 2
  • 24
  • 41
0

Almost correct, you need not cast it to list

import re 
x = ['Accara building model (ABM)','tri-com model (tcm)']
y = []
for i in x:
    y.append(re.sub(r'\([^)]*\)', '', i))

print (y)
stark
  • 399
  • 2
  • 13
0

Being Pythonic doesn't need to be fewest possible lines of code. Explaining Explicit is Better than Implicit from the Zen of Python

x = ['Accara building model (ABM)','tri-com model (tcm)']
result=[]
for i in x:
    result.append(r.sub(r'\(.*\)','',i))
S.N
  • 4,910
  • 5
  • 31
  • 51