Discuss / Python / 记录

记录

Topic source

1090

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

1、

def normalize(name):

    return name.title()

2、

def prod(L):

    def fx(a,b):

       return a*b

    return reduce(fx,L)

3、

def str2float(s):

    DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}  

    def fn(x, y):

        return x * 10 + y

    def char2num(s):

        return DIGITS[s]

    # 将字符串按小数点分割成整数部分和小数部分

    parts = s.split('.')

    integer = reduce(fn, map(char2num, parts[0]))

    decimal = reduce(fn, map(char2num, parts[1])) / (10 ** len(parts[1]))

    return integer + decimal

开“福”

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

你的第三个太长了

def str2float(s):

s_int = s.split('.')[0]

s_float = '0.' + s.split('.')[1]

return reduce(lambda x, y: x + y, map(float, [s_int, s_float]))


  • 1

Reply