Discuss / Python / 小白练习,感觉代码比较精简了,往各位指正

小白练习,感觉代码比较精简了,往各位指正

Topic source

HUFFIE

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

T1

def normalize(name):
    return name[0].upper() + name[1:].lower()

T2

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

T3

num_dict = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
def char2num(s):
    return num_dict[s]
def str2float(s):
    s1, s2 = s.split('.')
    return reduce(lambda x, y: x * 10 + y, map(char2num, s1)) + reduce(lambda x, y: x / 10 + y, map(char2num, s2[::-1])) / 10

蝉的夏

#2 Created at ... [Delete] [Delete and Lock User]
def str2float(s):    s1, s2 = s.split('.')    return reduce(lambda x, y: x * 10 + y, map(lambda n: DIGITS[n], (s1 + s2))) / (10 ** len(s2))

蝉的夏

#3 Created at ... [Delete] [Delete and Lock User]
DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}

  • 1

Reply