Discuss / Python / 菜鸟报道,不知道str.find() ,利用了一下Python非基本变量的特性,写出了C语言的感觉......

菜鸟报道,不知道str.find() ,利用了一下Python非基本变量的特性,写出了C语言的感觉......

Topic source

True_Believer

#1 Created at ... [Delete] [Delete and Lock User]
# -*- coding: utf-8 -*-
from functools import reduce
d = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '.': '.'}
N = [0]
def char2nums(ch):
    return d[ch]
def add(num1, num2):
    if num2 == '.':
        N[0] = 1
        return num1
    if N[0] == 0:
        return num1 * 10 + num2
    if N[0] >= 1:
        num1 = num1 + num2 *0.1** N[0]
        N[0] += 1
        return num1
def str2float(s):
    x = reduce(add, map(char2nums, s))
    N[0] = 0
    return x

  • 1

Reply