简体   繁体   English

忽略带有括号的列表中的单词的最有效(pythonic)方法是什么?

[英]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.

相关问题 什么是将字符串拆分为连续,重叠的单词列表的最pythonic方法 - What is the most pythonic way to split a string into contiguous, overlapping list of words 大多数pythonic(和有效)的方式将列表成对嵌套 - Most pythonic (and efficient) way of nesting a list in pairs 修复此列表的最pythonic /最有效的方法是什么? - Whats the most pythonic / efficient way to fix this list? 计算一个单词与列表中其他单词的距离的最有效方法是什么? - What is the most efficient way to calculate the distance of a word with the other words in a list? 假设我在Python中有一个列表。 按部分将其随机化的最有效的Python方法是什么? - Suppose I had a list in Python. What's the most pythonic and efficient way to randomize it by sections? 列表边界-最Python的方式是什么? - List boundaries - what is the most Pythonic way? 重新编写大熊猫列的最有效和pythonic方法是什么? - What is the most efficient & pythonic way to recode a pandas column? 在组合上增加迭代的pythonic或最有效的方法是什么? - What is the pythonic or most efficient way to increase an iteration over combinations? 在 pandas 中计算 limsup/liminf 的最有效和 Pythonic 方法是什么? - What is the most efficient and Pythonic way of calculating limsup/liminf in pandas? 在匹配元素处划分列表的 Pythonic 和有效方法是什么? - What is a Pythonic and efficient way to divide a list at a matching element?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM