-2

New to Python.

I'd like to remove the substrings between the word AND and the comma character in the following string:

MyString = ' x.ABC AND XYZ, \ny.DEF AND Type, \nSome Long String AND Qwerty, \nz.GHI AND Tree \n'

The result should be:

MyString = ' x.ABC,\ny.DEF,\nSome Long String,\nz.GHI\n'

I'd like to do it without using regex.

I have tried various methods with splits and joins and indexes to no avail.

Any direction appreciated.

Thanks.

David Zemens
  • 53,033
  • 11
  • 81
  • 130
A.G.
  • 2,089
  • 3
  • 30
  • 52

3 Answers3

1

You can split the string into lines, and further split the lines into words and use itertools.takewhile to drop all words after AND (itself included):

from itertools import takewhile

''.join(' '.join(takewhile(lambda x: x != 'AND', line.split())) + ',\n' 
                                      for line in MyString.splitlines())

Notice that the newline character and a comma are manually added after each line is reconstructed with str.join.

All the lines are then finally joined using str.join.

Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
1

While Moses's answer is really good, I have a funny feeling this is a homework question and meant for you not to use any imports. Anyways here's an answer with no imports, it's not as efficient as other answers like Moses' or Regex but it works just not as well as others.

MyString = 'x.ABC AND XYZ, \ny.DEF AND Type, \nSome Long String AND Qwerty, \nz.GHI AND Tree \n'
new_string = ''
for each in [[y for y in x.split(' AND ')][0] for x in MyString.split('\n')]:
    new_string+=each
    new_string+='\n'

print(new_string)
MooingRawr
  • 4,901
  • 3
  • 24
  • 31
0

Now it is working.. and probably avoiding the 'append' keyword makes it really fast...

In [19]: ',\n'.join([x.split('AND')[0].strip() for x in MyString.split('\n')])


Out[19]: 'x.ABC,\ny.DEF,\nSome Long String,\nz.GHI,\n'

You can check this answer to understand why...

Comparing list comprehensions and explicit loops (3 array generators faster than 1 for loop)

Community
  • 1
  • 1
Riccardo Petraglia
  • 1,943
  • 1
  • 13
  • 25