python - 从具有多个值的字典中获取单个键的值的键
问题描述
给定一本字典
collision_domain = {0: [0, 1, 2], 1: [2, 3, 4]}
此代码应输出domain 0
:
search = 1
for dom, node in collision_domain.items():
if node == search:
print("domain", dom)
我们如何得到它?
解决方案
我们在这里需要做的是在循环的每次迭代中在列表中进行搜索。
请注意,搜索值首先出现,然后是列表。
if search in node:
最终代码:
for dom, node in collision_domain.items():
if search in node:
print("domain", dom)
推荐阅读
- c# - unity可移动Menu界面。碰撞检测问题
- python - 如何在python数据框中为2列添加前缀€符号和后缀%符号
- lua - 检查 Lua 中超过 2 个值之间的相等性
- hadoop - hadoop.tmp.dir 不能在正确的位置工作
- matlab - 测量 lsqnonlin 的 Hessian 矩阵
- inno-setup - 如何防止 Inno Setup 在 TEMP 中创建文件?
- java - 如何从字节数组或文件或字符串或任何东西中获取资源?
- javascript - 在 vue.js 文件中导入音频 .wav 文件时出现错误
- android - Firebase 实时数据库规则在身份验证和发送数据后不起作用
- angular - NgFor 仅支持绑定到 Iterables,例如 Arrays Angular 11