Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to prevent a surplus of the substring used as separator by re.split from being left inside one of the resulting list items after string splitting?

import re

input_text = "Creo que ((PERS)los viejos gabinetes) estan en desuso, hay que hacer algo con ellos, ya que ellos son importantes. ellos quedaron en el deposito de ellos. ellos ((PERS)los cojines) son acolchonados, ellos estan sobre el sofá. creo que ellos estan sobre el sofá"

pattern = r"\(\(PERS\)\s*los\s*"
matches = re.findall(pattern, input_text)
result = re.split(pattern, input_text)

final_result = []
for i in range(len(matches)):
    if i == 0:
        final_result.append(result[i] + matches[i])
    else:
        final_result.append(matches[i-1] + result[i] + matches[i])
final_result.append(matches[-1] + result[-1])

final_result = [x.strip() for x in final_result if x.strip() != '']

print(final_result)

When dividing the strings to form the list, it makes the mistake of leaving a part of ((PERS)los in the element before the separation.

And consequently I get this wrong output:

['Creo que ((PERS)los', '((PERS)los viejos gabinetes) estan en desuso, hay que hacer algo con ellos, ya que ellos son importantes. ellos quedaron en el deposito de ellos. ellos ((PERS)los', '((PERS)los cojines) son acolchonados, ellos estan sobre el sofá. creo que ellos estan sobre el sofá']

Here is the problem:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

que ((PERS)los', '((PERS)los viej

s. ellos ((PERS)los', '((PERS)los cojines) son ac

This should be the correct output:

['Creo que ', '((PERS)los viejos gabinetes) estan en desuso, hay que hacer algo con ellos, ya que ellos son importantes. ellos quedaron en el deposito de ellos. ellos ', '((PERS)los cojines) son acolchonados, ellos estan sobre el sofá. creo que ellos estan sobre el sofá']

>Solution :

I think you’re overcomplicating the problem. Why not use split with a look-ahead as pattern?

import re

input_text = "Creo que ((PERS)los viejos gabinetes) estan en desuso, hay que hacer algo con ellos, ya que ellos son importantes. ellos quedaron en el deposito de ellos. ellos ((PERS)los cojines) son acolchonados, ellos estan sobre el sofá. creo que ellos estan sobre el sofá"
pattern = re.compile(r"(?=\(\(PERS\)\s*los\s*)")
print(re.split(pattern, input_text))

Output.

['Creo que ', '((PERS)los viejos gabinetes) estan en desuso, hay que hacer algo con ellos, ya que ellos son importantes. ellos quedaron en el deposito de ellos. 
ellos ', '((PERS)los cojines) son acolchonados, ellos estan sobre el sofá. creo que ellos estan sobre el sofá']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading