May*_*ava 0 python json dictionary python-2.7
我有一个像这样的格式的字典
d = {
"Fruit_1" : ["mango", "apple"],
"Fruit_2" : ["apple"],
"Fruit_3" : ["mango", "banana", "apple", "kiwi", "orange"]
}
Run Code Online (Sandbox Code Playgroud)
我传递一个值为"芒果",我想得到所有相应的键,只有芒果出现.我无法在有价值的地方获得相应的密钥.
迭代d.items并检查mango值的存在.
In [21]: [key for key,value in d.items() if 'mango' in value]
Out[21]: ['Fruit_1', 'Fruit_3']
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1321 次 |
| 最近记录: |