Discuss / Python / 作业

作业

Topic source

南墙小生

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

T1:

L1 = ['adam', 'LISA', 'barT']
L2 = list(map(lambda name: name.capitalize(), L1))

T2:

def prod(L):
    return reduce(lambda x, y: x * y, L)

print(prod([3, 5, 7, 9]))

T3:

def str2int(s):
    def char2num(c):
        return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[c]
    return reduce(lambda x, y: x *10 + y, map(char2num, s))

def str2float(s):
    s_list = s.split('.')
    float_i = str2int(s_list[0])
    float_f = str2int(s_list[1]) / (10**len(s_list[1]))
    return float_i + float_f

print(str2float('123.456'))

看答案才知道capitalize(),谢谢大神

请问大神第三道题后面的{'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[c] 这个[c]是什么意思?


  • 1

Reply