python - 这是什么意思?

标签 python

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/

相关文章:

python - 如何使用 OpenCV 理解 Python 中的头部姿势估计角度?

python - PyQt:使用 setRect() 控制 QGraphicsRectItem 的位置

python - 如何在 python 中使用 enumerate() 枚举字典中的项目

python - 如何在不检查页面的情况下让 pelican 包含 content/some_dir?

python - 如果超参数调整的影响很小,我该如何改进我的 XGBoost 模型?

python - 如何将 ffmpeg 命令转换为 ffmpeg python 代码

python - Python 中的素数和完美平方检查器

python - 从 synset 获取同义词返回错误 - Python

python 无法识别 windows 10

python - pyzmq 应该使用哪种协议(protocol)?