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])) if prod([3, 5, 7, 9]) == 945: print('测试成功!') else: print('测试失败!')
DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
def char2num(s): return DIGITS[s]
def str2float(s): dev = s.find('.') s = s[:dev] + s[dev+1:] return reduce(lambda x, y: x * 10 + y, map(char2num, s)) / pow(10, len(s) - dev)
print('str2float(\'123.456\') =', str2float('123.456')) if abs(str2float('123.456') - 123.456) < 0.00001: print('测试成功!') else: print('测试失败!')
print('3 5 7 * 9 =', prod([3, 5, 7, 9]))
这一行的*号被文本格式吃掉了,大家自己注意加上。。。
def normalize(name): return str.capitalize(name)
#capitalize 使用内置函数真的就高效吗?
Sign in to make a reply
wenggz
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])) if prod([3, 5, 7, 9]) == 945: print('测试成功!') else: print('测试失败!')
DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
def char2num(s): return DIGITS[s]
def str2float(s): dev = s.find('.') s = s[:dev] + s[dev+1:] return reduce(lambda x, y: x * 10 + y, map(char2num, s)) / pow(10, len(s) - dev)
print('str2float(\'123.456\') =', str2float('123.456')) if abs(str2float('123.456') - 123.456) < 0.00001: print('测试成功!') else: print('测试失败!')