Discuss / Python / 第三题免char2num版本

第三题免char2num版本

Topic source

春之逝水

#1 Created at ... [Delete] [Delete and Lock User]
from functools import reduce

def str2float(s):
    a, b = s.split('.')
    return int(a) + pow(10, -len(b))*int(b)

抑郁胖子

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

有感而发

from functools import reduce

def str2float(s):
    a, b = s.split('.')
    return int(a) + int(b)/(10**len(b))

_Ljj110719

#3 Created at ... [Delete] [Delete and Lock User]
  • 这样。。。太偷懒吧。。你们这就没有用到这节的MapReduce啊:(

  • 廖老师这节教程的最后一段就是假设没有int() 之类的函数嘛。。

    也就是说,假设Python没有提供int()函数,你完全可以自己写一个把字符串转化为整数的函数,而且只需要几行代码

  • 更偷懒的方法。。那还不如直接用float(),一行代码就够了

    >>> print(float('123.456'))
    123.456
    

  • 1

Reply