Python:if j['category']中的abc

2024-09-30 22:19:24 发布

您现在位置:Python中文网/ 问答频道 /正文

给定一个如下所示的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"
    }
]

有没有可能在没有循环的情况下,检查其中任何一个中是否存在“blue”?你知道吗

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

Tags: 文件importjsonfalsetrue情况greenblue
1条回答
网友
1楼 · 发布于 2024-09-30 22:19:24

正如@jonrsharpe提到的,您最好的选择可能是使用^{}

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

或者

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

相关问题 更多 >