Discuss / Python / str2float

str2float

Topic source
# -*-coding:utf-8-*-
from functools import reduce


def str2float(l):
    def get_digit(x):
        if str.isdigit(x):
            return int(x)

    def prod(x, y):
        return x*10+y
    n = len(l) - str.index(l, '.')-1
    ol = list(map(get_digit, l))
    ol.remove(None)

    return reduce(prod, ol)/(10**n)


  • 1

Reply