Search code examples
pythonstringnumbers

Find specific value in string python


How can I find all numbers different of zero in a such string. for example:

'\\0\\0.412583\\0.4415169\\0.4140446\\0.3750735\\0\\0.4128125\\0\\0'

I tried to use regex but it does not work

Thanks in advance


Solution

  • If you're regex allergic, you can use str.split with a classical listcomp.

    Assuming (s) is your string, try this :

    listOfNumbers = [float(n) for n in s.split("\\") if n not in ["", "0"]]
    

    Output : ​

    print(listOfNumbers)
    #[0.412583, 0.4415169, 0.4140446, 0.3750735, 0.4128125]