第三题比较简单的做法
Topic sourcemap的作用是传入的函数一次作用到序列的每个元素.
比如传进去的s0是 字符串:'123'
所以 map(Integer, s0), 就是让传进去的字符串中的每个元素(map函数的第二个参数是Iterable, 因此字符串的Iterable的每个元素就是'1','2','3'), 传进Integer函数的元素'1','2','3', 这时就用到了`你问的List1`了.
**通过dict中的key来获取value, 其中key是传入的每个元素'1','2','3', 而获取的value为1, 2, 3. 并且将这三个值通过新的Iterator返回 -> 123**
这就是`List1的作用`
这里把测试用例换成12340.456的时候 结果不对
修改了下层主代码 如下
def str2float(s):
decimal_places: int = len(s) - s.index('.') - 1
num_enum = {'1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '0': 0, '.': '.'}
def fn(x, y) -> int:
if y != '.':
return x * 10 + y
else:
return x
return reduce(fn, map(lambda x: num_enum[x], s)) / (10 ** decimal_places)
- 1
韦着选躬