Discuss / Python / Daily Work

Daily Work

Topic source

无愠无殇

#1 Created at ... [Delete] [Delete and Lock User]
#####################题一###################
from functools import reduce

def normalize(name):
    name = name[0].upper() + name[1:].lower()
    return name
L1 = ['adam', 'LISA', 'barT']
L2 = list(map(normalize, L1))
#print(L2)

#####################题二###################
def prod(L):
    return reduce(lambda x, y : x * y ,L)
#print('3 * 5 * 7 * 9 =', prod([3, 5, 7, 9]))

#####################题三###################
# split()函数说明
# a = 'name:haha,age:20|name:python,age:30|name:fef,age:55'
#
# print a.split('|')
#
# 返回结果:
#
# ['name:haha,age:20', 'name:python,age:30', 'name:fef,age:55']

def str2float(s):
    def char2num(s):
        return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s]
    Ln = s.split('.')
    m = reduce(lambda x, y: x * 10 + y, map(char2num, Ln[0]))
   ###reversed()将小数部分反转,得到正常相加的结果
    n = reduce(lambda x, y: x * 0.1 + y, map(char2num, reversed(Ln[1])))
    return m + n * 0.1
    # print(Ln ,m + n * 0.1)
print('str2float(\'123.456\') =', str2float('123.456'))
print(123.456==str2float('123.456'))

看到现在还是大神你的解法最好


  • 1

Reply