首页 > 解决方案 > 在 python 字典中搜索所有值

问题描述

我正在用python做一个解析器。但我不知道如何在字典结构中一路向下搜索。

可以说我希望能够获取所有包含 None 的值。

{data01: 'full', data02:'None', data03:'high', data04:'None'}

我尝试过以下方法:

if None in inputfromjson.values():
    excludekey

标签: pythonjsonpython-3.xdictionaryparsing

解决方案


通过这种方式,您可以找出所有为 None 的键。

a = {'data01': 'full', 'data02': None, 'data03': 'high', 'data04': None}
for key,value in a.items():
    if value == None:
        print(key)

a.items() 是一个容器,它返回一个元组,包含键及其对应的值。


推荐阅读