Discuss / Python / 交作业

### 交作业

Topic source

#### 回风哥哥

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

``````def normalize(name):
return f'{name[0].upper()}{name[1:].lower()}'
``````

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

``````def str2float(s):
"""字符串转浮点型"""

def char2num(c):
"""字符转数字"""

digists = {'0':0, '1':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9}
return digists[c]

def profix_num(l):
"""数字列表转数字"""

return reduce(lambda x, y: x * 10 + y, l)

def subfix_num(l):
"""数字列表转小数"""

return reduce(lambda x, y: x * 0.1 + y, l) * 0.1

pro_str, sub_str = s.split('.')[0], s.split('.')[1]  # 获得整数部分和小数部分字符串
pro_num = profix_num(map(char2num, pro_str))  # 获得整数部分
sub_num = subfix_num(map(char2num, sub_str[::-1]))  # 获得小数部分
return pro_num + sub_num
``````

#### 回风哥哥

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

``````def str2float(s):
"""字符串转浮点型"""

def char2num(c):
"""字符转数字"""

digists = {'0':0, '1':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9}
return digists[c]

s_split = s.split('.')
pro_num = profix_num(map(char2num, s_split[0]))  # 获得整数部分
sub_num = subfix_num(map(char2num, s_split[1][::-1]))  # 获得小数部分
return pro_num + sub_num
``````

#### 回风哥哥

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

``````def str2float(s):
"""字符串转浮点型"""

def char2num(c):
"""字符转数字"""

digists = {'0':0, '1':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9}
return digists[c]

s_split = s.split('.')

pro_num = reduce(lambda x, y: x * 10 + y, map(char2num, s_split[0]))  # 获得整数部分
sub_num = reduce((lambda x, y: x * 0.1 + y) * 0.1, map(char2num, s_split[1][::-1]))  # 获得小数部分
return pro_num + sub_num
``````

#### onespringday_xx

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

#### 努力吧咚咚锵

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

``````def normalize(name):    return str.lower(name)L1 = ['adam', 'LISA', 'barT']L2 = list(map(normalize, L1))print(L2)
``````

#### 非诚勿扰74685

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

s_split[1][::-1]是先索引再翻转，并不是切片，切片是[:x]这样的形式。希望4楼的兄弟能看到

• 1