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 replace the exact string and not string part?

How to replace the exact string and not a part of the string, please?

l = ['1.5', '12.3', '.', 'A', '.', '.']
l = [i.replace('.', 'nan') for i in l]
print(l)

I obtain:

['1nan5', '12nan3', 'nan', 'A', 'nan', 'nan']

Desired result:

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

['1.5', '12.3', 'nan', 'A', 'nan', 'nan']

>Solution :

You aren’t replacing in the string, your doing a string comparison

 ['nan' if i == '.' else i for i in l]
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