Discuss / Python / 我也做了一个方法

我也做了一个方法

Topic source

witnessus

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

廖老师的这份 Python 教程绝对不是给新手用的… (另:似乎网站不支持 Markdown 标记...)

def str2float( s ):
    '''A function used for invert str to float.'''

    def to_num( s ):
    # The ASCII codec for '.' is 0x2E, namely 46(radix 10).
        return {'0':0, '1':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7,'8':8, '9':9, '.':46}[s]

       # i is used for counting numbers which is larger than 1.
    i = 0
    L = list( map( tonum, s ) )

    for x in L:
        i = i + 1
        if x == 46:
            break

    print(L.remove(46))
    return reduce( lambda x, y: x * 10 + y, L ) / 10 **(len(L) - i + 1)

  • 1

Reply