Dictionary is below
my = {
"1": {
"exclude": 'A,B',
"column": "value",
"output": "Out1",
"Out1": "Cost",
"Out2": "Rev"
},
"2": {
"exclude": 'None',
"column": "value",
"output": "Out2",
"Out1": "Cost",
"Out2": "Rev"
}
}
exclude_list = ['A','B','C']
exclude_list = ['A','B','C']
take the value B,C
. If the exclude key is 'A,B' take the value C
as shown in the example.output
key if output
key is Out1
then fetch value of Out1
and mapped to EndOutput
Expected Out
{'1': {'exclude': ['C'], 'EndOutput': 'Cost'},
'2': {'exclude': ['A', 'B', 'C'], 'EndOutput': 'Rev'}}
exclude_list = ['A','B','C']
for k,v in my.items():
if v['column'].lower() == 'value'.lower:
You can iterate over keys 1,2 .. N with for e in my:
and then calculate exclude with help of set()
difference as
and extract things like this
exc = set(['A','B','C']) - set(my[e]["exclude"].split(",") if len(my[e]["exclude"].split(",")) else [] )
next you can take value of out1
or out2
depending on the value of output
as
my[e][my[e]["output"]]
Insert in new dictionary as
newdict[e] = {'exclude': list(exc) ,'EndOutput':my[e][my[e]["output"]]}
Overall, this is how you can iterate
newdict = {}
for e in my:
if my[e]['column'].lower() == 'value':
exc = set(['A','B','C']) - set(my[e]["exclude"].split(",") if len(my[e]["exclude"].split(",")) else [] )
newdict[e] = {'exclude': list(exc) ,'EndOutput':my[e][my[e]["output"]]}
printing newdict will produce this output
'1': {'EndOutput': 'Cost', 'exclude': ['C'] },
'2': {'EndOutput': 'Rev', 'exclude': ['A', 'B', 'C'] }}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments