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 remove repeated sentences from a string

I have an issue that I do not know how to tackle.

For example: I have a string returning in a function that has multiple sentences separatade by a comma. And some of them are comming repeated:

Like:

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

"lorem ipsum dolor, lorem ipsum dolor, lorem ipsum dolor"

I need to remove these sentences that are comming repeated but without checking word-by-word, rather sentence by sentence striped by ",". Since there may have other sentences with repeated words that should not be removed.

Input example:

"lorem ipsum dolor, lorem ipsum dolor, lorem mark dol"

Output desired:

"lorem ipsum dolor, lorem mark dol"

>Solution :

This solution is based on the Tim Roberts comment. The only difference is OrderedDict usage in order to preserve sentences order:

from collections import OrderedDict

string = 'lorem ipsum dolor, lorem ipsum dolor, lorem mark dol'
string = ', '.join(OrderedDict.fromkeys(string.split(', ')))
print(string)

Output:

lorem ipsum dolor, lorem mark dol
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