Python 清理数据-如何删除两个单词和括号()之间的斜杠(/)

Python 清理数据-如何删除两个单词和括号()之间的斜杠(/),python,regex,python-3.x,string,data-cleaning,Python,Regex,Python 3.x,String,Data Cleaning,我对编程和Python相当陌生。我有一个字符串列表: ['Iraqi', 'Freedom/Operation', 'New', 'Dawn', 'and', 'Operation', 'Enduring', 'Freedom', '(Afghanistan),', 'have', '(other', 'than', 'call', 'publications)'] 如何清除两个单词之间的斜杠和任何单词/单词中的括号。一个干净的数据将是: ['Iraqi', 'Freedom', 'Oper

我对编程和Python相当陌生。我有一个字符串列表:

['Iraqi', 'Freedom/Operation', 'New', 'Dawn', 'and', 'Operation', 'Enduring',
 'Freedom', '(Afghanistan),', 'have', '(other', 'than', 'call', 'publications)']
如何清除两个单词之间的斜杠和任何单词/单词中的括号。一个干净的数据将是:

['Iraqi', 'Freedom', 'Operation', 'New', 'Dawn', 'and', 'Operation', 'Enduring',
 'Freedom', 'Afghanistan,', 'have', 'other', 'than', 'call', 'publications']
你可以试试这个

\w+匹配任何等于[a-zA-Z0-9_]的字字符

输出:

['Iraqi', 'Freedom', 'Operation', 'New', 'Dawn', 'and', 'Operation', 'Enduring',
 'Freedom', 'Afghanistan,', 'have', 'other', 'than', 'call', 'publications']
不使用re。可以使用str.strip和str.split


使用列表理解:

a = ['Iraqi', 'Freedom/Operation', 'New', 'Dawn', 'and', 'Operation', 'Enduring',
 'Freedom', '(Afghanistan),', 'have', '(other', 'than', 'call', 'publications)']


b = [ i.split('/') for i in a]
b = [ i for row in b for i in row]
b = [ i.strip().strip(',').strip('(').strip(')') for i in b]

print(b)
['Iraqi', 'Freedom', 'Operation', 'New', 'Dawn',
 'and', 'Operation', 'Enduring', 'Freedom',
 'Afghanistan', 'have', 'other', 'than',
 'call', 'publications']
让我列出你的名单:

a = ['Iraqi', 'Freedom/Operation', 'New', 'Dawn', 'and', 'Operation', 'Enduring',
 'Freedom', '(Afghanistan),', 'have', '(other', 'than', 'call', 'publications)']
首先,用斜线分隔所有元素

c = [j for elem in  for j in elem.split("/") ]

And now all in one,
c=[j代表a中的元素,j代表re.subr'[],elem.split/]


其次,假设您希望从列表中的每个元素中删除一组字符,例如[,]

为此,您可以构建正则表达式:

d = [re.sub(r'[(\)]', "", elem) for elem in c]
结果是

['Iraqi', 'Freedom', 'Operation', 'New', 'Dawn', 'and', 'Operation', 
'Enduring', 'Freedom', 'Afghanistan,', 'have', 'other', 'than', 'call', 'publications']
请看看这个

data_list = ['Iraqi', 'Freedom/Operation', 'New', 'Dawn', 'and', 'Operation', 'Enduring',
 'Freedom', '(Afghanistan),', 'have', '(other', 'than', 'call', 'publications)']

out_put_list = []
for data in data_list:
    if '/' in data:
        out_put_list.extend(data.split("/"))
    else:
        out_put_list.append(data.replace('(', '').replace(')', ''))

print(out_put_list)

欢迎来到堆栈溢出!根据你自己的研究,了解到目前为止你累了什么会有帮助;更换接头、接头等?请注意,我们要求在此处输入stackoverflow
['Iraqi', 'Freedom', 'Operation', 'New', 'Dawn', 'and', 'Operation', 
'Enduring', 'Freedom', 'Afghanistan,', 'have', 'other', 'than', 'call', 'publications']
data_list = ['Iraqi', 'Freedom/Operation', 'New', 'Dawn', 'and', 'Operation', 'Enduring',
 'Freedom', '(Afghanistan),', 'have', '(other', 'than', 'call', 'publications)']

out_put_list = []
for data in data_list:
    if '/' in data:
        out_put_list.extend(data.split("/"))
    else:
        out_put_list.append(data.replace('(', '').replace(')', ''))

print(out_put_list)