Discuss / Python / 我照着评论区大神的理解了一下,嘻嘻,s1,s2=split('.')是真的好用,直接整数小数部分分开

我照着评论区大神的理解了一下,嘻嘻,s1,s2=split('.')是真的好用,直接整数小数部分分开

Topic source

徐轲l

#1 Created at ... [Delete] [Delete and Lock User]

from functools import reduce

def str2float(s):

    def text2(x,y):

        return x*10+y

    def text3(x,y):

        return x/10+y

    s1,s2=s.split('.')#根据'.'将str分成两个子串

    s2=s2[::-1]#取倒序

    a1=reduce(text2,map(int,s1))

    a2=reduce(text3,map(int,s2))/10

    return a1+a2


  • 1

Reply