[英]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.使用 re 我能够忽略括号中的单词。 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你不应该使用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.顺便说一句:我使用strip()
删除末尾的空格。 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(' (')
顺便说一句:如果您确定 elments 将始终具有相同的结构,那么您可以在没有正则表达式的情况下删除它,但使用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输出: output
列表看起来像
["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.当您使用list(re.sub(...))
您基本上是将输出字符串(替换后)转换为列表格式。
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. Pythonic 不需要尽可能少的代码行。 Explaining Explicit is Better than Implicit from the Zen of Python从 Python 之禅解释显式优于隐式
x = ['Accara building model (ABM)','tri-com model (tcm)']
result=[]
for i in x:
result.append(r.sub(r'\(.*\)','',i))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.