Get path of all the elements in dictionary
我在python中有一个字典,想要获取值param1或param2的路径,…用其他值替换这些值。比如说,要获取param1的值,我需要像获取['optionsettings'][2]['value']一样获取它。我可以用一些通用代码来做这个吗?它将打印我所有节点/叶的路径下面是字典
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | { "ApplicationName":"Test", "EnvironmentName":"ABC-Nodejs", "CNAMEPrefix":"ABC-Neptune", "SolutionStackName":"64bit Amazon Linux 2016.03 v2.1.1 running Node.js", "OptionSettings": [ { "Namespace":"aws:ec2:vpc", "OptionName":"AssociatePublicIpAddress", "Value":"true" }, { "Namespace":"aws:elasticbeanstalk:environment", "OptionName":"EnvironmentType", "Value":"LoadBalanced" }, { "Namespace":"aws:ec2:vpc", "OptionName":"Subnets", "Value":"param1" }, { "Namespace":"aws:autoscaling:launchconfiguration", "OptionName":"SecurityGroups", "Value":"param2" }, { "Namespace":"aws:autoscaling:asg", "OptionName":"MinSize", "Value":"1" }, { "Namespace":"aws:autoscaling:asg", "OptionName":"MaxSize", "Value":"4" }, { "Namespace":"aws:autoscaling:asg", "OptionName":"Availability Zones", "Value":"Any" }, { "Namespace":"aws:autoscaling:asg", "OptionName":"Cooldown", "Value":"360" }, { "Namespace":"aws:autoscaling:launchconfiguration", "OptionName":"IamInstanceProfile", "Value":"NepRole" }, { "Namespace":"aws:autoscaling:launchconfiguration", "OptionName":"MonitoringInterval", "Value":"5 minutes" }, { "Namespace":"aws:autoscaling:launchconfiguration", "OptionName":"RootVolumeType", "Value":"gp2" }, { "Namespace":"aws:autoscaling:launchconfiguration", "OptionName":"RootVolumeSize", "Value":"10" }, { "Namespace":"aws:elasticbeanstalk:sns:topics", "OptionName":"Notification Endpoint", "Value":"[email protected]" }, { "Namespace":"aws:elasticbeanstalk:hostmanager", "OptionName":"LogPublicationControl", "Value":"false" }, { "Namespace":"aws:elasticbeanstalk:command", "OptionName":"DeploymentPolicy", "Value":"Rolling" }, { "Namespace":"aws:elasticbeanstalk:command", "OptionName":"BatchSizeType", "Value":"Percentage" }, { "Namespace":"aws:elasticbeanstalk:command", "OptionName":"BatchSize", "Value":"100" }, { "Namespace":"aws:elasticbeanstalk:command", "OptionName":"HealthCheckSuccessThreshold", "Value":"Ok" }, { "Namespace":"aws:elasticbeanstalk:command", "OptionName":"IgnoreHealthCheck", "Value":"false" }, { "Namespace":"aws:elasticbeanstalk:command", "OptionName":"Timeout", "Value":"600" }, { "Namespace":"aws:autoscaling:updatepolicy:rollingupdate", "OptionName":"RollingUpdateEnabled", "Value":"false" }, { "Namespace":"aws:ec2:vpc", "OptionName":"ELBSubnets", "Value":"param3" }, { "Namespace":"aws:elb:loadbalancer", "OptionName":"SecurityGroups", "Value":"param4" }, { "Namespace":"aws:elb:loadbalancer", "OptionName":"ManagedSecurityGroup", "Value":"param4" } ] } |
这基本上是同一个问题:
为python嵌套字典的所有值打印完整的键路径
它本质上是必要的代码。您需要添加的是对列表的处理,因为您不只是提供一个键/值结构,可以通过以下问题来解决:
如何在python中检查变量是否为字典
它将向您展示如何检查类型并根据数据类型修改行为。实现这一点的最简单方法是设置两个虚拟数据类型,一个定义为空列表,另一个定义为空映射,并对虚拟类型的元素执行"isInstance"检查,以决定要执行的逻辑。
在这两个答案之间,您应该能够构建您正在寻找的路径。
迷路是对的,这不是一个"为我写"的网站。
这对我有用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | def dict_traverse(self,obj,path): if isinstance(obj, dict): for k,v in obj.items(): if isinstance(v, str): #if v.find('param')!=-1: print(path+"["+"'"+k+"'"+"]",v) elif isinstance(v, list): for elem in v: self.myvar+=1 path_lst="["+"'"+k+"'"+"]"+"["+str(self.myvar)+"]" self.dict_traverse(elem,path_lst) elif isinstance(obj, list): for elem in obj: self.dict_traverse(elem,path) ['SolutionStackName'] 64bit Amazon Linux 2016.03 v2.1.1 running Node.js ['OptionSettings'][1]['OptionName'] AssociatePublicIpAddress ['OptionSettings'][1]['Namespace'] aws:ec2:vpc ['OptionSettings'][1]['Value'] true ['OptionSettings'][2]['OptionName'] EnvironmentType ['OptionSettings'][2]['Namespace'] aws:elasticbeanstalk:environment ['OptionSettings'][2]['Value'] LoadBalanced ['OptionSettings'][3]['OptionName'] Subnets ['OptionSettings'][3]['Namespace'] aws:ec2:vpc ['OptionSettings'][3]['Value'] param1 ['OptionSettings'][4]['OptionName'] SecurityGroups ['OptionSettings'][4]['Namespace'] aws:autoscaling:launchconfiguration ['OptionSettings'][4]['Value'] param2 ['OptionSettings'][5]['OptionName'] MinSize ['OptionSettings'][5]['Namespace'] aws:autoscaling:asg ['OptionSettings'][5]['Value'] 1 ['OptionSettings'][6]['OptionName'] MaxSize ['OptionSettings'][6]['Namespace'] aws:autoscaling:asg ['OptionSettings'][6]['Value'] 4 ['OptionSettings'][7]['OptionName'] Availability Zones ['OptionSettings'][7]['Namespace'] aws:autoscaling:asg ['OptionSettings'][7]['Value'] Any ['OptionSettings'][8]['OptionName'] Cooldown ['OptionSettings'][8]['Namespace'] aws:autoscaling:asg ['OptionSettings'][8]['Value'] 360 ['OptionSettings'][9]['OptionName'] IamInstanceProfile ['OptionSettings'][9]['Namespace'] aws:autoscaling:launchconfiguration ['OptionSettings'][9]['Value'] NepRole ['OptionSettings'][10]['OptionName'] MonitoringInterval ['OptionSettings'][10]['Namespace'] aws:autoscaling:launchconfiguration ['OptionSettings'][10]['Value'] 5 minutes ['OptionSettings'][11]['OptionName'] RootVolumeType ['OptionSettings'][11]['Namespace'] aws:autoscaling:launchconfiguration ['OptionSettings'][11]['Value'] gp2 ['OptionSettings'][12]['OptionName'] RootVolumeSize ['OptionSettings'][12]['Namespace'] aws:autoscaling:launchconfiguration ['OptionSettings'][12]['Value'] 10 ['OptionSettings'][13]['OptionName'] Notification Endpoint ['OptionSettings'][13]['Namespace'] aws:elasticbeanstalk:sns:topics ['OptionSettings'][13]['Value'] sunil.kumar2@pb.com ['OptionSettings'][14]['OptionName'] LogPublicationControl ['OptionSettings'][14]['Namespace'] aws:elasticbeanstalk:hostmanager ['OptionSettings'][14]['Value'] false ['OptionSettings'][15]['OptionName'] DeploymentPolicy ['OptionSettings'][15]['Namespace'] aws:elasticbeanstalk:command ['OptionSettings'][15]['Value'] Rolling ['OptionSettings'][16]['OptionName'] BatchSizeType ['OptionSettings'][16]['Namespace'] aws:elasticbeanstalk:command ['OptionSettings'][16]['Value'] Percentage ['OptionSettings'][17]['OptionName'] BatchSize ['OptionSettings'][17]['Namespace'] aws:elasticbeanstalk:command ['OptionSettings'][17]['Value'] 100 ['OptionSettings'][18]['OptionName'] HealthCheckSuccessThreshold ['OptionSettings'][18]['Namespace'] aws:elasticbeanstalk:command ['OptionSettings'][18]['Value'] Ok ['OptionSettings'][19]['OptionName'] IgnoreHealthCheck ['OptionSettings'][19]['Namespace'] aws:elasticbeanstalk:command ['OptionSettings'][19]['Value'] false ['OptionSettings'][20]['OptionName'] Timeout ['OptionSettings'][20]['Namespace'] aws:elasticbeanstalk:command ['OptionSettings'][20]['Value'] 600 ['OptionSettings'][21]['OptionName'] RollingUpdateEnabled ['OptionSettings'][21]['Namespace'] aws:autoscaling:updatepolicy:rollingupdate ['OptionSettings'][21]['Value'] false ['OptionSettings'][22]['OptionName'] ELBSubnets ['OptionSettings'][22]['Namespace'] aws:ec2:vpc ['OptionSettings'][22]['Value'] param3 ['OptionSettings'][23]['OptionName'] SecurityGroups ['OptionSettings'][23]['Namespace'] aws:elb:loadbalancer ['OptionSettings'][23]['Value'] param4 ['OptionSettings'][24]['OptionName'] ManagedSecurityGroup ['OptionSettings'][24]['Namespace'] aws:elb:loadbalancer ['OptionSettings'][24]['Value'] param4 ['EnvironmentName'] ABC-Nodejs ['CNAMEPrefix'] ABC-Neptune ['ApplicationName'] Test |
您可以尝试实现类似于广度优先搜索或深度优先搜索的东西。