-1

I am pretty new to programming and Python. I have a list of string:

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

How do I clean all the slash between two words and the bracket enclosed in any word/words. A clean data would be:

['Iraqi', 'Freedom', 'Operation', 'New', 'Dawn', 'and', 'Operation', 'Enduring',
 'Freedom', 'Afghanistan,', 'have', 'other', 'than', 'call', 'publications']
Ch3steR
  • 15,132
  • 4
  • 20
  • 45
Sean
  • 3
  • 1
  • 2
    Welcome to stack overflow! It would help to know what you have tired so far based on your own research; `re.sub`, `str.replace`, etc? Note that we ask for a [mcve] here on stackoverflow – G. Anderson Mar 06 '20 at 16:13

4 Answers4

2

You can try this.

\w+ matches any word character (equal to [a-zA-Z0-9_])

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

new=re.findall('\w+',' '.join(lst))

Output:

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

Without using re. You can use str.strip() and str.split().

[i.strip('()') for s in lst for i in s.split('/')]
Ch3steR
  • 15,132
  • 4
  • 20
  • 45
0

Let me name your list:

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

First, to separate all elements by the slash you can do

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

And now all in one,

c = [j for elem in a for j in re.sub(r'[()]', "", elem).split("/") ]


Second, let's say that you want to remove a set of charachters from each element from the list, for example ['(',')']

For this you can build a regular expression:

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

And the result is

['Iraqi', 'Freedom', 'Operation', 'New', 'Dawn', 'and', 'Operation', 
'Enduring', 'Freedom', 'Afghanistan,', 'have', 'other', 'than', 'call', 'publications']
BCJuan
  • 605
  • 5
  • 12
0

Please check out this.

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)
krishna
  • 914
  • 3
  • 11
-1

Using list comprehension:

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']
Bhishan Poudel
  • 1
  • 9
  • 63
  • 108