我在python中有一个字典,想要获取值的路径param1或param2,...用其他值替换它们。说得到param1的价值我需要像[' OptionSettings'] [2] ['价值']那样得到它。我可以使用一些通用代码来执行此操作,这将打印所有节点/叶子的路径 下面是字典
{
"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": "sunil.kumar2@pb.com"
},
{
"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"
}
]
}
答案 0 :(得分:1)
这基本上与此问题相同:
Print complete key path for all the values of a python nested dictionary
它本质上是必要的代码。你需要添加的是对列表的处理,因为你没有简单地介绍一个键/值结构,这可以通过这个问题解决:
How to check if a variable is a dictionary in python
这将向您展示如何根据数据类型检查类型和修改行为。完成此操作的最简单方法是设置两个虚拟数据类型,一个定义为空列表,另一个定义为空映射,并根据虚拟类型执行元素的“isinstance”检查,以确定您想要的逻辑执行。
在这两个答案之间,您应该能够构建您正在寻找的路径。
Lost是对的,这不是“为我写的”网站。
答案 1 :(得分:1)
我已经创建了一个“扁平化”字典/列表嵌套对象的功能。
def flatten_dict_list(obj, path):
if isinstance(obj, (str, int, float)):
return [(path, obj)]
elif isinstance(obj, dict):
ret = []
for k, v in obj.items():
ret.extend(flatten_dict_list(v, path + [k]))
return ret
elif isinstance(obj, list):
ret = []
for (n, elem) in enumerate(obj):
ret.extend(flatten_dict_list(elem, path + [str(n)]))
return ret
else:
raise ValueError("supported value types: str, int, float")
您可以像这样使用此功能
>>> data = {"key1": [1, 2, 3], "key2": {"a": "A", "b": "B", "C": ["c1", "c2"]}}
>>> {"[" + '] ['.join(k) + ']' : v for (k, v) in flatten_dict_list(data, [])}
{'[key1] [0]': 1,
'[key1] [1]': 2,
'[key1] [2]': 3,
'[key2] [a]': 'A',
'[key2] [b]': 'B',
'[key2] [C] [0]': 'c1',
'[key2] [C] [1]': 'c2'}
答案 2 :(得分:0)
这对我有用
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
答案 3 :(得分:-1)
您可以尝试实现类似于广度优先搜索或深度优先搜索的内容。