Discuss / Python / 练习题

练习题

Topic source
输入:['adam', 'LISA', 'barT'],输出:['Adam', 'Lisa', 'Bart']:
 def normalize(name):
     x=name.capitalize()     # 把第一个字母转化为大写字母,其余小写
     return x
测试:
L1 = ['adam', 'LISA', 'barT']
L2 = list(map(normalize, L1))
print(L2)
Python提供的sum()函数可以接受一个list并求和,请编写一个prod()函数,可以接受一个list并利用reduce()求积
from functools import reduce
def prod(L):
    def mul(x,y):
        return x*y
    return reduce(mul, L)
print('3 * 5 * 7 * 9 =', prod([3, 5, 7, 9]))
if prod([3, 5, 7, 9]) == 945:
    print('测试成功!')
else:
    print('测试失败!')
利用map和reduce编写一个str2float函数,把字符串'123.456'转换成浮点数123.456
from functools import reduce
def str2float(s):
    def str2num(x, y):
        return x * 10 + y
    def char2num(s):
        DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
        return DIGITS[s]
    s=s.split('.')
    float_x=reduce(str2num, map(char2num, s[0]))+
    (0.1**len(s[1]))*reduce(str2num, map(char2num, s[1]))
    return float_x

你好,请问(0.1len(s[1]))*reduce(str2num, map(char2num, s[1]))里面的 是什么意思啊

“**”是什么意思。。谢谢


  • 1

Reply