from functools import reduce
def str2int(s):
def fn(x, y):
return x * 10 + y
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]
return reduce(fn, map(char2num, s))
我正在学习函数 map() 和 reduce()。 {...}[.] 是什么意思?
最佳答案
它的意思很简单:构建字典并从键返回特定值。
print({'foo': 0, 'bar': 1}['foo']) # output : 0
print({'foo': 0, 'bar': 1}['bar']) # output : 1
关于python - 这是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34660723/