Python 在有妄想症的列表中,忽略单词最有效的方法是什么?
我有以下清单Python 在有妄想症的列表中,忽略单词最有效的方法是什么?,python,regex,python-3.x,string,list,Python,Regex,Python 3.x,String,List,我有以下清单 x = ['Accara building model (ABM)','tri-com model (tcm)'] 使用re,我可以忽略括号中的单词。如下 import re x = ['Accara building model (ABM)','tri-com model (tcm)'] for i in x: ko= list(re.sub("[\(\[].*?[\)\]]", "", i)) print (ko) 但是我得到了以下格式的输出 ['A',
x = ['Accara building model (ABM)','tri-com model (tcm)']
使用re,我可以忽略括号中的单词。如下
import re
x = ['Accara building model (ABM)','tri-com model (tcm)']
for i in x:
ko= list(re.sub("[\(\[].*?[\)\]]", "", i))
print (ko)
但是我得到了以下格式的输出
['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', ' ']
理想情况下,我想要的是尽可能少的代码行。(我知道我的代码目前效率低下)
理想输出要求
['Accara building model', 'tri-com model']
你就快到了,试试这个:
import re
x = ['Accara building model (ABM)','tri-com model (tcm)']
output = []
for i in x:
ko= re.sub("[\(\[].*?[\)\]]", "", i)
output.append(ko)
输出:输出
列表如下所示
["Accara building model", "tri-com model"]
使用list(re.sub(…)
时,基本上是将输出字符串(替换后)转换为列表格式。import re
x=[‘阿克拉建筑模型(ABM)’,‘tri-com模型(tcm)’]
打印([“”.join(列表(re.sub([\(\[].[\)\]],“”,i)),用于x中的i])
python测试
[‘阿克拉建筑模型’、‘tri com模型’]
您不应该使用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)
结果
['Accara building model', 'tri-com model']
您甚至可以使用列表理解
import re
x = ['Accara building model (ABM)','tri-com model (tcm)']
results = [re.sub("[\(\[].*?[\)\]]", "", i).strip() for i in x]
print(results)
顺便说一句:我使用
strip()
删除末尾的空格。但是您可以使用regex删除这个空格,regex以空格“[\(\[].?[\)\]]”
开头
编辑:正如Mark Meyer在评论中建议的那样,您也可以编译正则表达式-这样它就不必在每个循环中都编译正则表达式
x = ['Accara building model (ABM)','tri-com model (tcm)']
pattern = re.compile(" [\(\[].*?[\)\]]")
results = [re.sub(pattern, "", i) for i in x]
print(results)
顺便说一句:如果您确信elments将始终具有相同的结构,那么您可以使用
split(“(”)
几乎正确,您不需要将其强制转换为列表
import re
x = ['Accara building model (ABM)','tri-com model (tcm)']
y = []
for i in x:
y.append(re.sub(r'\([^)]*\)', '', i))
print (y)
Pythonic不需要尽可能少的代码行。从Python的禅中解释显式比隐式好
因为使用了
list(),所以可以获得字符
。您应该在loo之前创建空列表,并将结果添加到此列表中。谢谢。我想您的上一个解决方案对我的案例来说是最有效的,因为它涉及的代码相对较少。无需额外创建空列表和添加。如果效率有问题,也许在循环外编译re是个好主意。
import re
x = ['Accara building model (ABM)','tri-com model (tcm)']
y = []
for i in x:
y.append(re.sub(r'\([^)]*\)', '', i))
print (y)
x = ['Accara building model (ABM)','tri-com model (tcm)']
result=[]
for i in x:
result.append(r.sub(r'\(.*\)','',i))