python - 获取键对应于字典中的值

标签 python dictionary

我在 python 中有这本字典

d={1: 'a', 2: 'b', 3: 'c'}

用 d[1] 我得到

>>> d[1]
'a'

如何让键对应一个值?
示例:使用 'a' 得到 1

最佳答案

你想反转映射:

如图this question

dict((v,k) for k, v in map.iteritems())

关于python - 获取键对应于字典中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8650415/

相关文章:

python - 根据特定键合并两个字典列表

python - 如何从字典中替换数据框列中的特定值?

python - 障碍物不会继续生成。最初只产生一个然后循环被切断?

python - 如何在 iPython 中将变量作为命令行参数传递?

python - ffmpeg concat在python中具有不同的编解码器

python - Python线程中的heapq安全吗?

python - 在 Pytest 运行之前执行一些代码

python - 添加具有相同键的值

java - 为什么 Map 不能使用日期作为键?

dictionary - 从 Go 中的映射条目创建平面数组