Python:如果 abc 在 j ['category' ]

标签 python json list python-2.7

给定一个如下所示的 JSON 文件:

"foobar": [
    {
        "a": "true",
        "b": 1,
        "c": 1234,
        "d": 9,
        "e": "red"
    },
    {
        "a": "false",
        "b": 2,
        "c": 2345,
        "d": 7,
        "e": "green"
    },
    {
        "a": "whocares",
        "b": 3,
        "c": 3456,
        "d": 5,
        "e": "blue"
    }
]

是否可以在没有循环的情况下检查“蓝色”是否出现在任何中?

import simplejson
j = json.loads(superfile().text)
if "blue" in j['foobar'][ANYONE]['e']

最佳答案

作为@jonrsharpe提到,你最好的选择可能是使用 any , 例如

if any('blue' in i['e'] for i in j['foobar'])

或者

if any('blue' == i['e'] for i in j['foobar'])

关于Python:如果 abc 在 j ['category' ],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27425621/

相关文章:

python - 如何在 python 中查找特定键的值

python - 使用隐马尔可夫模型进行情感分析

python - 是否可以在开头没有介绍文本的情况下运行 Python REPL?

java - 使用 Jackson 解析 JSON Bing 结果

python - 在嵌套的python dict中查找所有出现的键

list - Haskell [x] 和 x 符号 - 作为模式示例

python - 如何为 Python 3.7.2 安装 Pycrypto?

javascript - 引用 JSON (Javascript) 对象的元素

C 程序错误 - 段错误(核心已转储)

python - LabelEncoder 和 zip python 的问题