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 extract partial number from a list of strings?

I have a list of strings

['apple_orange_percentage_0.709_0.752',
 'apple_orange_percentage_0.752_0.786',
 'apple_orange_percentage_0.673_0.709']

I need just a list of the unique numbers here:

[0.673, 0.709, 0.752, 0.786] 

Could someone guide me on how to get there?

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

>Solution :

If you’re not concerned about the order of the values in your output list then:

data = ['apple_orange_percentage_0.709_0.752',
 'apple_orange_percentage_0.752_0.786',
 'apple_orange_percentage_0.673_0.709']

s = set()

for sv in data:
    s.update(map(float, sv.split('_')[-2:]))

print(list(s))

output:

[0.673, 0.752, 0.709, 0.786]
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