Discuss / Python / 交作业

交作业

Topic source

_潴潴俠_

#1 Created at ... [Delete] [Delete and Lock User]
def normalize(name):
    return  name.capitalize()
r = map(normalize,['adam', 'LISA', 'barT'])
print(list(r))

from functools import reduce

def product(x,y):
    return  x*y
l = reduce(product,[3, 5, 7, 9])
print('3 * 5 * 7 * 9 =', l)

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]

def str2int(s):
    sl = s.split('.') #以小数点分割字符串
    strint   = reduce(lambda x, y: x *10+y, map(char2num, sl[0])) #整数部分
    strfloat = reduce(lambda x, y: x * 0.1+y, map(char2num, sl[1]))*0.1 #小数部分
    return strint + strfloat

print(str2float('1245.5'))

LeesLee峰

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

小数部分顺序颠倒了..


  • 1

Reply