Discuss / Python / 交作业

交作业

Topic source
在此插入代码
第三题

    def add(x,y):
        return x*10 + y
    def cee(x,y):
        return x * 0.1 + y

    a = list(s)
    b = a.remove('.')
    c = map(int,a)
    d = list(c)
    e = d[::-1]
    f = reduce(add, d[:3])
    g = reduce(cee, e[:3]) * 0.1
    return f+g


零基础学了几天,不知道这样的代码是不是很糟糕?
还有想问问大神们,这个 {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s]
是什么意思?

def float(x): L1 = x[:x.index('.')] L2 = x[:x.index('.'):-1] def c2n(y): return {'1':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, '0':0}[y] def fn(x,y): return x * 10 + y def fnn(x,y): return x / 10 + y return reduce(fn, map(c2n, L1)) + (reduce(fnn, map(c2n, L2)))/ 10

那个是dict【】索引的意思,我也是初学者:)


  • 1

Reply