python - 有什么方法可以从 python 中的值中获取键?(字典)
问题描述
这个问题似乎有点奇怪,因为我知道字典不打算根据值而不是键来使用我最近遇到了某些问题,以尽量减少表格填写过程,因为如果提供了某些国家城市,那么我需要自动填写国家和大陆那个用户。我得到以下 Json 供我参考。
[{
"Asia": {
"Japan": [
"tokyo",
"hirohima",
]
}
},
{
"Europe": {
"England": [
"Manchester",
"London",
"South gate",
]
}
}
]
假设用户城市是伦敦,我可以得到英格兰和欧洲作为输出吗?还是我的 json 格式错误?
解决方案
您可以遍历数据并“逆向工程”字典:
city = "London"
for d in data:
for continent, countries in d.items():
for country, cities in countries.items():
if "London" in cities:
print(country, continent)
推荐阅读
- javascript - js中的for循环记录另一个索引
- php - 如何在设定的日期和时间的预订事件之间创建查询开放时间段?
- mysql - Sql 查询索引无法正常工作
- excel - UserForm 在创建新数据之前检查现有列表
- java - AtomicLong 不能在线程之间准确传递
- tensorflow - tf.nn.sigmoid_cross_entropy_with_logits 是否将数据转换为概率?
- php - 如何在 php 中将图像从 mysql 显示到表中
- python-3.x - Tensorflow - 除了现有的模型训练之外的自定义模型训练
- java - 在 spark sql java 中不起作用
- c# - 在 C# 中查找最近创建的 Active Directory 用户