简体   繁体   中英

What is the most efficient (pythonic) way to ignore words in a list that has parantheses?

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

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)

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.

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

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM