Issue
I have a list of multi dictionary currently looks like
result: [
{
"title": {
"en": "Farm Added Successfully!"
},
"message": {
"en": "Time to add Crops to your farm!"
},
"icon_link": {
"en": null
},
"image_link": {
"en": null
},
"message_code": 1002,
"module": "Farm Added"
},
{
"title": {
"hi": "खेत सफलतापूर्वक जुड़ गया है!"
},
"message": {
"hi": "अभी अपने खेत में फसल जोड़ें|"
},
"icon_link": {
"hi": null
},
"image_link": {
"hi": null
},
"message_code": 1003,
"module": "Pending task today"
},
{
"title": {
"en": "Check out today's Tasks NOW!"
},
"message": {
"en": "Tasks are important for your crop health!"
},
"icon_link": {
"en": null
},
"image_link": {
"en": null
},
"message_code": 1003,
"module": "Pending task today"
},
How can I merge the dictionary "title", "image", "icon" and "message", columns based on the condition when the "message_code" matches with the other dictionary "message_code"
and have the desired output as
"results": [
{
"message_code": 1002,
"module": "Farm Added",
"title": {
"en": "Farm Added Successfully!",
"hi": "खेत सफलतापूर्वक जुड़ गया है!"
},
"message": {
"en": "Time to add Crops to your farm!"
"hi": "अभी अपने खेत में फसल जोड़ें|"
},
"icon_link": {
"en": null,
"hi": null
},
"image_link": {
"en": null,
"hi": null
}
},
{
"message_code": 1003,
"module": "Pending task today",
"title": {
"en": "Check out today's Tasks NOW!",
"hi": "आज का कार्य अभी देखें!"
},
"message": {
"en": "Tasks are important for your crop health!",
"hi": "आपकी फसल के स्वास्थ्य के लिए कार्य महत्वपूर्ण हैं!"
},
"icon_link": {
"en": null,
"hi": null
},
"image_link": {
"en": null,
"hi": null
}
},
The code for this which I am using in Django gets API
notification =[]
input_dict = {}
notification_data = get_notification_template(filter)
notification_data = notification_data.replace({np.nan: None})
for index, row in notification_data.iterrows():
input_dict['message_code'] = row['message_code']
input_dict['module'] = row['action_at']
if row['message_code'] and row['action_at'] in input_dict.values():
input_dict = {
'title': {row['language_code']: row['title']},
'message': {row['language_code']: row['message']},
'icon_link': {row['language_code']: row['icon_link']},
'image_link': {row['language_code']: row['image_link']},
}
else:
input_dict = {
'message_code': row['message_code'],
'module': row['action_at'],
'title': {row['language_code']: row['title']},
'message': {row['language_code']: row['message']},
'icon_link': {row['language_code']: row['icon_link']},
'image_link': {row['language_code']: row['image_link']},
}
notification.append(input_dict)
Here get_notification_template is a function that gets the raw data from the database and here I am creating a dictionary to show the data as per the required format
Solution
Try something likes this (the code can be optimized):
input_dicts = [
{'message_code': 1002, 'module': '1002', 'title': {'en': 'en'}, 'message': {'en': 'en'}, 'icon_link': {'en': 'en'}, 'image_link': {'en': 'en'}},
{'message_code': 1002, 'module': '1002', 'title': {'es': 'es'}, 'message': {'es': 'es'}, 'icon_link': {'es': 'es'}, 'image_link': {'es': 'es'}},
{'message_code': 1003, 'module': '1003', 'title': {'en': 'en'}, 'message': {'en': 'en'}, 'icon_link': {'en': 'en'}, 'image_link': {'en': 'en'}}
]
message_codes = {d['message_code'] for d in input_dicts}
output_dicts = []
for message_code in message_codes:
dicts_with_code = [d for d in input_dicts if d['message_code'] == message_code]
output_dicts.append({'message_code': message_code, 'module': dicts_with_code[0]['module']})
for key in ['title', 'message', 'icon_link', 'image_link']:
output_dicts[len(output_dicts) - 1][key] = {}
for dict_with_code in dicts_with_code:
for k, v in dict_with_code[key].items():
output_dicts[len(output_dicts) - 1][key][k] = v
print(output_dicts)
Answered By - Alain Bianchini
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.