Discuss / Python / 作业

作业

Topic source

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

L1 = ['adam', 'LISA', 'barT'] L2 = list(map(normalize, L1)) print(L2)

from functools import reduce

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): return reduce(lambda x, y: x * 10 + y, map(char2num, s))

print(str2int('123'))

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

""" 利用map和reduce编写一个str2float函数,把字符串'123.456'转换成浮点数123.456 """

def str2float(s):

str1,str2 = s.split('.')

#

return str1

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))) / 10 ** len(s1)

print(str2float('123.456'))


  • 1

Reply