Discuss / Python / 作业

作业

Topic source

iq052

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

第一题

def normalize(s): return s[0].upper() + s[1:].lower() print(list(map(normalize,['adam', 'LiSa', 'barT'])))

第二题

print(reduce(lambda x, y:x * y,[1, 2, 3, 4, 5, 6]))#

第三题

def str2float(s): n = s.index('.')

#n = s.find('.')
s1 = s[:n]
s2 = s[n + 1:][::-1] + '0'#取小数点后的数字反序后加0
#print (s1,s2)
return reduce(lambda x, y: x + y, 
              [
                  reduce(lambda x, y: x * 10 + y, map(lambda x: int(x), s1)), 
                  reduce(lambda x, y: x / 10 + y, map(lambda x: int(x), s2)) 
              ])

print(str2float('123,444555666'))

第三题好简洁,我自己找了下find的语法:str.find(str, beg=0, end=len(string))


  • 1

Reply