Discuss / Python / 作业codes

作业codes

Topic source

抗抑指南

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

第一题

def normalize(name):
    return name.title()

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

第二题

from functools import reduce

def prod(L):
   def multiply(x,y):
       return x * y
   return reduce(multiply, L)

print('3 * 5 * 7 * 9 =', prod([3, 5, 7, 9]))

第三题

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 str2float(s):
    p = s.index('.')
    l = len(s)
    int = reduce(lambda x, y : x * 10 + y, map(char2num, s[0:p])) 
    dig = reduce(lambda x, y : x / 10 + y, map(char2num, s[(p+1):l][::-1]))
    return int + dig /10

print('str2float(\'123.456\') = ', str2float('123.456'))

请问,第一题name.title是一个函数吗? 在cmd>>python中敲入name和title都提示不是函数。

不好意思,百度到了title用法~请问,想了解title()内置函数的源码,去哪里找?


  • 1

Reply