是否有更多Pythonic方法来迭代通过字典键查找值而不是这个?

Cra*_*aig 3 python dictionary python-2.7

这是一个示例字典,colors:

{
    "Red" : {
        "members" : {
            "153950039532134112" : {
                "rank" : 1,
                "score" : 43,
                "time" : 1530513303
            }
        },
        "rank" : 2,
        "score" : 43
    },
    "Blue" : {
        "members" : {
            "273493248051539968" : {
                "rank" : 1,
                "score" : 849,
                "time" : 1530514923
            },
            "277645262486011904" : {
                "rank" : 2,
                "score" : 312,
                "time" : 1530513964
            },
            "281784064714487810" : {
                "rank" : 3,
                "score" : 235,
                "time" : 1530514147
            }
        },
        "rank" : 1,
        "score" : 1396
    }
}
Run Code Online (Sandbox Code Playgroud)

为了这个例子,我们假设这个字典中还有更多的颜色命名键.现在,假设我正在寻找特定的会员ID.

for key, value in colors.items():
    if member_id in value['members']:
        return True
Run Code Online (Sandbox Code Playgroud)

有没有更简单,可能是单行的方式来做到这一点?

Sel*_*cuk 7

这是另一个使用any耦合 生成器表达式的单行程序:

return any(member_id in color['members'] for color in colors.values())
Run Code Online (Sandbox Code Playgroud)