Discuss / Python / T3

Eye丶杯

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

from functools import reduce

def str2float( s ):

    def fn( x , y ):

        return x*10 + y 

    def char2num( s ):

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

        return digits[s]                            **#此前都与str2int相同**

    u1,u2 = s.split('.')                           ** #将s以'.'为节点分割为两个字符串**

    L1 = reduce ( fn ,map( char2num , u1 ))

    L2 = reduce ( fn ,map( char2num , u2 ))/(10**len(u2))

    return L1+L2

L2的计算有问题哦。因为L2 是整数间的相除,所以reduce后需要*1.0

即  L2 = reduce ( fn ,map( char2num , u2 )) * 1.0/(10**len(u2))

Lsdmz2019

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

请问下为什么整数间的相除就要*1.0?

Eye丶杯

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

同问


  • 1

Reply