- I am counting the occurrence of different
DocumentCode
which for now isBS, F16, SS
which can be different. - Input
ldct_input_body = {
"ContextKey": "Mr.Dave",
"Input": [
{
"DocumentCode": "BS",
"FileName": "SomethingOne",
"Object": "base64",
"ObjectFormat": "pdf"
},
{
"DocumentCode": "BS",
"FileName": "SomethingTwo",
"Object": "base64",
"ObjectFormat": "pdf"
},
{
"DocumentCode": "SS",
"FileName": "SomethingTwo",
"Object": "base64",
"ObjectFormat": "pdf"
},
{
"DocumentCode": "F16",
"FileName": "SomethingTwo",
"Object": "base64",
"ObjectFormat": "pdf"
}
]
}
- So I am creating a different dictionary by identifying the
DocumentCode
like below
ldct_document_code = dict()
ldct_document_code['ObjectEntries'] = []
for lstr_multiple_input_object in ldct_input_body["Input"]:
ldct_document_code['ObjectEntries'].append(lstr_multiple_input_object['DocumentCode'])
- Output
{
"ObjectEntries": [
"BS",
"BS",
"SS",
"F16"
]
}
- Now, I am looking to increase the count depending upon how many Inputs of same
DocumentCode
are present - I had a hard-coded code for it like below
for lstr_multiple_input_object in ldct_input_body["Input"]:
if lstr_multiple_input_object["DocumentCode"] == "BS":
lint_bank_statement_count = 1
lint_total_no_of_object = 1
elif lstr_multiple_input_object["DocumentCode"] == "F16":
lint_form_16_count = 1
lint_total_no_of_object = 1
elif lstr_multiple_input_object["DocumentCode"] == "SS":
lint_salary_slip_count = 1
lint_total_no_of_object = 1
- But I want it to accept dynamic value of ObjectEntries, get it and then compare it with the Input values.
- Expected Output
{
"ObjectEntries": [
"BS": 2,
"SS": 1,
"F16": 1
]
}
CodePudding user response:
See if this simple code snippet solves your purpose:
ldct_input_body = {
"ContextKey": "Mr.Dave",
"Input": [
{
"DocumentCode": "BS",
"ObjectFormat": "pdf"
},
{
"DocumentCode": "BS",
"ObjectFormat": "pdf"
},
{
"DocumentCode": "SS",
"ObjectFormat": "pdf"
},
{
"DocumentCode": "F16",
"ObjectFormat": "pdf"
}
]
}
ldct_document_code = {}
output_dict = {}
for x in ldct_input_body['Input']:
for k, v in x.items():
if v == "pdf": continue
if output_dict.get(v, None) is None:
output_dict[v] = 1
else:
output_dict[v] = output_dict[v] 1
ldct_document_code['ObjectEntries'] = output_dict
print(ldct_document_code)
OUTPUT:
{'ObjectEntries': {'BS': 2, 'SS': 1, 'F16': 1}}