我有以下列方式看的字典
{
"modules": [
{
"outputs": {
"AdminAZ1": "value1",
"AdminAZ2": "value2",
"Region": "value3"
}
}]
}
我的问题我需要找到关键"输出"并返回true
如果确实需要以下列方式输出
"outputs": {
"AdminAZ1": "value1",
"AdminAZ2": "value2",
"Region": "value3"
}
答案 0 :(得分:2)
这应该可以帮到你
dict ={
"modules": [
{
"outputs": {
"AdminAZ1": "value1",
"AdminAZ2": "value2",
"AWSRegion": "value6"
}
}]
}
#check if outputs is present
print reduce(lambda x,y: x or y, map(lambda x: "outputs" in x.keys(),dict["modules"]), False)
#return outputs if present
print reduce(lambda x,y: x if x else y, map(lambda x: x["outputs"] if "outputs" in x.keys() else None,dict["modules"]), None)
答案 1 :(得分:0)
mydict={
"modules": [
{
"outputs": {
"AdminAZ1": "value1",
"AdminAZ2": "value2",
"Region": "value3"
}
}]}
result = mydict[mydict.keys()[0]][0]
if "outputs" in result:
print result
结果:
{
"outputs": {
"AdminAZ1": "value1",
"AdminAZ2": "value2",
"Region": "value3"
}